-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.babel.js
201 lines (168 loc) · 4.84 KB
/
app.babel.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
200
201
/* global __dirname */
/* global process */
"use strict";
import fs from "fs";
import https from "https";
import path from "path";
import express from "express";
import exphbs from "express-handlebars";
import handlebars from "handlebars";
import bodyParser from "body-parser";
import compression from "compression";
import session from "express-session";
import cookieParser from "cookie-parser";
import fileStore from "session-file-store";
// custom helpers
import { handleRender } from "./server/server";
import { requireHttps, requireWww } from "./server/helpers/routing.js";
import { getRoutes } from "./server/routes";
import { matchPath } from "react-router-dom";
import { SESSION_USER } from "./service/constants";
// configuration
const config = {
environment: process.env.NODE_ENV || "development",
isHttps: process.env.isHttps === true || false
};
const FileSessionStorage = fileStore(session);
const app = express();
app.use(compression());
const viewsDir = "./templates";
// setup express to use handlebars as the templating engine
const hbs = exphbs.create({
defaultLayout: "main",
layoutsDir: path.join(__dirname, `${viewsDir}/layouts`),
partialsDir: path.join(__dirname, `${viewsDir}/partials`),
extname: ".hbs"
});
// allows partials to be organised in subfolders
hbs
.getTemplates(path.join(__dirname, `${viewsDir}/partials`))
.then(function(partials) {
for (let partial in partials) {
handlebars.registerPartial(partial, "{{" + partial + "}}");
}
})
.catch(error => {
console.log(`Unable to retrieve templates. Error: ${error}`);
});
app.set("views", path.join(__dirname, `${viewsDir}`));
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
// setup server for static assets
app.use(
"/",
express.static(path.join(__dirname, "dist"), { maxAge: 604800000 })
);
// https://medium.com/@yash.kulshrestha/using-lets-encrypt-with-express-e069c7abe625
app.use("/.well-known", express.static(path.join(__dirname, ".well-known")));
// require HTTPS
app.use(requireHttps);
// redirect to include www
app.use(requireWww);
// Setup body parser for parsing POST request bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const sessionExpiration = 20 * 60 * 1000; // 20 minutes
app.use(cookieParser());
app.use(
session({
store: new FileSessionStorage({}),
secret: "auth",
cookie: { maxAge: sessionExpiration },
unset: "destroy",
resave: true,
saveUninitialized: false,
sameSite: true
})
);
app.post("/setSession", function(req, res) {
req.session[req.body.sessionKey] = req.body.sessionData;
if (req.body.rememberSession) {
req.session.cookie.maxAge = 365 * 24 * 60 * 60 * 1000;
}
res.end();
});
let extractPath = req => {
// extract the path from the url
let urlSections = req.path.split("/");
urlSections = urlSections.filter(sectionString => {
return sectionString.length > 0;
});
let urlPath = null;
if (urlSections.length === 0) {
if (urlSections[0] === "") {
}
urlPath = "/";
} else {
urlPath = "/" + urlSections.join("/");
}
return urlPath;
};
app.use((req, res, next) => {
if (req.path === "/logout/") {
req.session[SESSION_USER] = null;
return res.redirect("/login");
} else {
return next();
}
});
app.use((req, res, next) => {
if (process.env.AUTHENTICATION_MODE === "FORM") {
let user = req.session.user;
getRoutes().then(routes => {
let route = routes.find(item => {
return matchPath(req.path, {
path: item.url,
exact: true
});
});
if ((route && route.isPublic) || user) {
// If all is ok, move on to the router
return next();
}
if (!user) {
return res.redirect(302, "/login");
}
});
} else {
return next();
}
});
// React-Redux middleware
app.use(handleRender);
app.use(function(error, req, res, next) {
console.error(error.message);
res.status(500);
res.render("500", { layout: false });
return;
});
// use the environment's port or a random port
const port =
process.env.port ||
(process.env.isDev
? 3000
: Math.floor(Math.random() * (65535 - 1024)) + 1024);
app.listen(port, () => {
console.log(`Running ${config.environment} on localhost:${port}`);
});
if (config.isHttps) {
const options = {
key: fs.readFileSync("server.key"),
cert: fs.readFileSync("server.crt"),
requestCert: false,
rejectUnauthorized: false
};
// create a different random port for HTTPS
let httpsPort = process.env.isDev
? 6001
: Math.floor(Math.random() * 65535) + 1024;
while (httpsPort === port) {
httpsPort = Math.floor(Math.random() * 65535) + 1024;
}
const server = https.createServer(options, app).listen(httpsPort, () => {
console.log(
`Running ${config.environment} (HTTPS) on localhost:${httpsPort}`
);
});
}
module.exports = app;