-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
155 lines (134 loc) · 3.56 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const config = require('./config');
const express = require('express');
const path = require('path');
const fs = require('fs');
const PORT = process.env.PORT || process.argv[2] || 5001;
const API_URI = process.env.API_URI || process.argv[3] || 'http://localhost:5000/api';
const bodyParser = require('body-parser');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const Redis = require('ioredis');
const axios = require('axios');
const j2c = require('json2csv');
const mailgun = require('mailgun-js')({
apiKey: config.MAILGUN_KEY,
domain: config.MAILGUN_DOMAIN
})
let app = express();
let redis = new Redis({
hostname: config.REDIS_HOST,
port: config.REDIS_PORT,
password: config.REDIS_PASS
});
app.use(session({
store: new RedisStore({
client: redis
}),
secret: process.env.REDIS_SECRET,
resave: false,
saveUninitialized: true
}))
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
let checkAdmin = (req, res, next) => {
if(req.session && req.session.admin) {
return next();
}
return res.redirect('/login');
}
app.get('/', checkAdmin, (req, res) => {
console.log("Admin");
res.sendFile(path.resolve(__dirname, './views/admin.html'));
})
app.get('/login', (req, res) => {
res.sendFile(path.resolve(__dirname, './views/admin.html'));
})
app.post('/send-newsletter', checkAdmin, (req, res) => {
const data = {
from: `Mente Gee<noreply@imkreative.com>`,
to: req.body.email,
subject: req.body.subject,
html: req.body.html,
"o:tracking": 'yes',
"o:tag": req.body.tags || null
}
let response = {
error: false,
errorMessage: null,
errorCode: null,
mailgun: null
}
redis.hget('newsletters', req.body.uuid)
.then(result => {
let promise = new Promise((resolve, reject) => {
if(result) {
response.errorMessage = 'UUID already exists';
response.errorCode = 1000;
reject();
}
resolve();
})
return promise;
})
.then(result => {
redis.hset('newsletters', req.body.uuid, data.html);
return mailgun.messages().send(data);
})
.then(result => {
response.mailgun = result;
res.json(response);
})
.catch(error => {
response.error = true;
res.json(response);
})
})
app.get('/emails', checkAdmin, (req, res) => {
axios.get(`${API_URI}/get-email-list`)
.then(data => {
res.json(data.data.emails);
})
})
app.post('/login', (req, res) => {
console.log("Loggin In");
redis.get(req.body.username)
.then(result => {
if(result && result == req.body.password) {
req.session.admin = true;
return res.send('/');
}
res.send('/login?fail=true');
})
})
app.post('/logout', (req, res) => {
console.log("Loggin Out");
req.session.destroy();
res.end();
})
app.post('/signup', (req, res) => {
console.log("Signing Up");
redis.get(req.body.username)
.then(result => {
if(!result) {
if(req.body.username == config.ADMIN_USER) {
redis.set(config.ADMIN_USER, req.body.password);
req.session.admin = true;
return res.send('/');
}
return res.send('/login?fail=true');
}
return res.send('/login?fail=true');
})
})
if(process.env.NODE_ENV == "production"){
app.listen(PORT, process.env.PROD_IP, () => {
console.log('Connected on Production');
console.log('PORT: ', PORT);
})
}
else{
app.listen(PORT, () => {
console.log('Connected on Dev');
})
}