-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (132 loc) · 5.17 KB
/
app.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
import createError from "http-errors";
import express from "express";
import { abuseCheck } from "@cityssm/express-abuse-points";
import compression from "compression";
import path from "path";
import cookieParser from "cookie-parser";
import csurf from "csurf";
import rateLimit from "express-rate-limit";
import session from "express-session";
import FileStore from "session-file-store";
import * as configFunctions from "./helpers/configFunctions.js";
import * as stringFns from "@cityssm/expressjs-server-js/stringFns.js";
import * as dateTimeFns from "@cityssm/expressjs-server-js/dateTimeFns.js";
import { version } from "./version.js";
import { updateOnly as handler_updateOnly } from "./handlers/permissionHandlers.js";
import routerLogin from "./routes/login.js";
import routerDocuShare from "./routes/docuShare.js";
import routerExport from "./routes/export.js";
import routerContracts from "./routes/contracts.js";
import routerAdmin from "./routes/admin.js";
import * as databaseInitializer from "./helpers/databaseInitializer.js";
import debug from "debug";
const debugApp = debug("contract-expiration-tracker:app");
const __dirname = ".";
databaseInitializer.initContractsDB();
export const app = express();
if (!configFunctions.getProperty("reverseProxy.disableEtag")) {
app.set("etag", false);
}
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(abuseCheck({
byXForwardedFor: configFunctions.getProperty("reverseProxy.blockViaXForwardedFor"),
byIP: !configFunctions.getProperty("reverseProxy.blockViaXForwardedFor")
}));
if (!configFunctions.getProperty("reverseProxy.disableCompression")) {
app.use(compression());
}
app.use((request, _response, next) => {
debugApp(request.method + " " + request.url);
next();
});
app.use(express.json());
app.use(express.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(csurf({
cookie: true
}));
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 1000
});
app.use(limiter);
const urlPrefix = configFunctions.getProperty("reverseProxy.urlPrefix");
if (urlPrefix !== "") {
debugApp("urlPrefix = " + urlPrefix);
}
app.use(urlPrefix, express.static(path.join(__dirname, "public")));
app.use(urlPrefix + "/lib/bulma-js", express.static(path.join(__dirname, "node_modules", "@cityssm", "bulma-js", "dist")));
app.use(urlPrefix + "/lib/bulma-webapp-js", express.static(path.join(__dirname, "node_modules", "@cityssm", "bulma-webapp-js", "dist")));
app.use(urlPrefix + "/lib/date-diff", express.static(path.join(__dirname, "node_modules", "@cityssm", "date-diff", "es2015")));
app.use(urlPrefix + "/lib/fa5", express.static(path.join(__dirname, "node_modules", "@fortawesome", "fontawesome-free")));
const sessionCookieName = configFunctions.getProperty("session.cookieName");
const FileStoreSession = FileStore(session);
app.use(session({
store: new FileStoreSession({
path: "./data/sessions",
logFn: debug("contract-expiration-tracker:session"),
retries: 10
}),
name: sessionCookieName,
secret: configFunctions.getProperty("session.secret"),
resave: true,
saveUninitialized: false,
rolling: true,
cookie: {
maxAge: configFunctions.getProperty("session.maxAgeMillis"),
sameSite: "strict"
}
}));
app.use((request, response, next) => {
if (request.cookies[sessionCookieName] && !request.session.user) {
response.clearCookie(sessionCookieName);
}
next();
});
const sessionChecker = (request, response, next) => {
if (request.session.user && request.cookies[sessionCookieName]) {
return next();
}
return response.redirect(urlPrefix + "/login");
};
app.use(function (request, response, next) {
response.locals.configFunctions = configFunctions;
response.locals.dateTimeFns = dateTimeFns;
response.locals.stringFns = stringFns;
response.locals.user = request.session.user;
response.locals.csrfToken = request.csrfToken();
response.locals.buildNumber = version;
response.locals.urlPrefix = configFunctions.getProperty("reverseProxy.urlPrefix");
next();
});
app.get(urlPrefix + "/", sessionChecker, (_request, response) => {
response.redirect(urlPrefix + "/contracts");
});
app.use(urlPrefix + "/export", routerExport);
app.use(urlPrefix + "/contracts", sessionChecker, routerContracts);
if (configFunctions.getProperty("docuShare.isEnabled")) {
app.use(urlPrefix + "/docuShare", sessionChecker, routerDocuShare);
}
app.use(urlPrefix + "/admin", sessionChecker, handler_updateOnly, routerAdmin);
app.use(urlPrefix + "/login", routerLogin);
app.get(urlPrefix + "/logout", (request, response) => {
if (request.session.user && request.cookies[sessionCookieName]) {
request.session.destroy(null);
request.session = undefined;
response.clearCookie(sessionCookieName);
}
response.redirect(urlPrefix + "/login");
});
app.use(function (_request, _response, next) {
next(createError(404));
});
app.use(function (error, request, response) {
response.locals.message = error.message;
response.locals.error = request.app.get("env") === "development" ? error : {};
response.status(error.status || 500);
response.render("error");
});
export default app;