-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
199 lines (161 loc) · 6.33 KB
/
server.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const express = require('express'),
app = express(),
common = require('./config/common.js'),
environmentVariables = common.config(),
passport = require('passport'),
passportConfig = require('./app/passportConfig'),
flash = require('connect-flash'),
appRouter = require('./app/routes.js')(express,environmentVariables),
bodyParser = require('body-parser'),
jsonParser = bodyParser.json(),
cookieParser = require('cookie-parser'),
csrf = require('csurf');
require('./config/logs');
require('dotenv').config();
const serverPort = (process.argv[2] && !isNaN(process.argv[2]) ? process.argv[2] : (process.env.PORT || 3001));
app.use(cookieParser());
app.use(csrf({cookie: true}));
app.use(function(req, res, next) {
res.removeHeader("X-Powered-By");
res.removeHeader("Server");
return next();
});
// =====================================
// SESSION
// =====================================
const sessionSettings = JSON.parse(process.env.THESESSION);
app.use(function(req, res, next) {
if (req.cookies['LoggedIn']){
res.cookie('LoggedIn',true,{ maxAge: sessionSettings.cookieMaxAge, httpOnly: true });
}
return next();
});
const session = require("express-session")
const RedisStore = require("connect-redis")(session);
const { createClient } = require("redis");
const { password, port, host } = sessionSettings;
const connectTimeout = 15000;
const redisClient = createClient({
legacyMode: true,
password,
socket: { connectTimeout, port, host, tls: process.env.NODE_ENV !== "development" },
});
redisClient.connect((err) => {
if (err) {
console.error("Redis client error:", err);
next(err);
} else {
next();
}
});
redisClient.on("connect", () => {
console.log("Redis client connected successfully");
});
redisClient.on("error", (error) => {
console.error("Redis client error:", error);
});
const redisStore = new RedisStore({ client: redisClient });
app.use(
session({
store: redisStore,
prefix: sessionSettings.prefix,
saveUninitialized: false,
secret: sessionSettings.secret,
key: sessionSettings.key,
resave: false,
rolling: true,
cookie: {
domain: sessionSettings.domain,
maxAge: sessionSettings.maxAge,
secure: "auto",
},
})
);
// =====================================
// VIEW AND LOCALS
// =====================================
app.set('view engine', 'ejs');
app.use(function (req, res, next) {
res.locals = {
piwikID: environmentVariables.live_variables.piwikId,
feedbackURL: environmentVariables.live_variables.feedbackURL,
service_public: environmentVariables.live_variables.Public,
start_url: environmentVariables.live_variables.startPageURL,
govuk_url: environmentVariables.live_variables.GOVUKURL,
caseManagementSystem: environmentVariables.live_variables.caseManagementSystem,
_csrf: req.csrfToken()
};
next();
});
// =====================================
// PASSPORT CONFIG
// =====================================
app.use(flash()); //use connect-flash for flash messages stored in session
app.use(passport.initialize());
app.use(passport.session()); //persistent login sessions
app.use(jsonParser);
app.use(bodyParser.urlencoded({
extended: true
}));
// =====================================
// JOB SCHEDULER
// =====================================
const schedule = require('node-schedule');
const jobs = require('./config/jobs.js');
// As there are 2 instances running, we need a random time, or two emails will be sent
// for accounts nearing expiration. (Flag will be set by time of 2nd job execution to stop duplicate)
const hourlyInterval = environmentVariables.userAccountSettings.jobScheduleHour
const randomSecond = Math.floor(Math.random() * 60);
const randomMin = Math.floor(Math.random() * 60); //Math.random returns a number from 0 to < 1 (never will return 60)
const jobScheduleRandom = randomSecond + " " + randomMin + " " + environmentVariables.userAccountSettings.jobScheduleHour + " * * *";
schedule.scheduleJob(jobScheduleRandom, function(){jobs.accountExpiryCheck()});
passportConfig(app, passport);
app.use('/api/user', appRouter);
//Automatically update passport strategy
var fs = require('fs-extra');
fs.copy(__dirname+'/data/strategy.js', __dirname+'/node_modules/passport-local/lib/strategy.js', function (err) {});
// =====================================
// GOV STYLES
// =====================================
app.use("/api/user/",express.static(__dirname + "/public"));
app.use("/api/user/styles",express.static(__dirname + "/styles"));
app.use("/api/user/fonts",express.static(__dirname + "/fonts"));
app.use("/api/user/images",express.static(__dirname + "/images"));
app.use("/api/user/js",express.static(__dirname + "/js"));
//Pull in images from GOVUK packages
fs.copy('node_modules/govuk_frontend_toolkit/images', 'images/govuk_frontend_toolkit', function (err) {
if (err) return console.error(err);
});
fs.mkdirs('images/govuk_frontend_toolkit/icons', function (err) {
if (err) return console.error(err);
});
fs.readdir('images/govuk_frontend_toolkit', function(err, items) {
for (var i=0; i<items.length; i++) {
if('images/govuk_frontend_toolkit/'+items[i].substr(0,5)=='images/govuk_frontend_toolkit/icon-' && items[i].substr(items[i].length-3,3)=='png'){
moveItem(items[i]);
}
}
});
function moveItem(item){
fs.move('images/govuk_frontend_toolkit/'+item, 'images/govuk_frontend_toolkit/icons/'+item,{ clobber: true }, function (err) {
if (err) return console.error(err);
});
}
// =====================================
// START APP
// =====================================
process.on('uncaughtException', (error, origin) => {
console.error('----- Uncaught Exception -----')
console.error(error)
console.error('----- Exception Origin -----')
console.error(origin)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('----- Unhandled Rejection -----')
console.error(`Promise: ${promise}`)
console.error(`Reason: ${reason}`)
})
app.listen(serverPort);
console.log('Server started on port ' + serverPort);
console.log(`user account cleanup job will run every ${hourlyInterval} hours at ${randomMin} minutes and ${randomSecond} seconds past the hour`);
module.exports.getApp = app;