-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
123 lines (91 loc) · 2.64 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
/* eslint-disable max-lines-per-function */
// ###### Configuration ######
const config = require("./lib/config");
const HOST = config.HOST;
const PORT = config.PORT;
const TEXTS = require("./locale/texts.json").texts;
const isProduction = (config.NODE_ENV === "production");
// ###### Express/Postgres modules ######
const express = require("express");
const flash = require("express-flash");
const session = require("express-session");
// ###### Postgres modules ######
const PgSession = require("connect-pg-simple")(session);
const PgStore = require("./model/pg-store");
// ###### App setuo ######
const app = express();
app.set("view engine", "pug");
app.set("views", "./views");
// Session setup
let sessionConfig = {
cookie: {
httpOnly: true,
maxAge: 180 * 24 * 3600000,
path: "/",
secret: false
},
name: "thrsqr-session-id",
resave: false,
saveUninitialized: false,
secret: config.SECRET,
store: new PgSession({
conObject: {
connectionString: config.DATABASE_URL,
ssl: isProduction ? { rejectUnauthorized: false } : false
}
})
};
if (isProduction) {
app.set("trust proxy", 1);
sessionConfig.cookie.secure = true;
}
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(session(sessionConfig));
app.use(flash());
// Create a new datastore
app.use((_req, res, next) => {
res.locals.store = new PgStore();
next();
});
// Detect and set language
app.use((req, res, next) => {
if (!req.session.language) {
req.session.language = req.headers["accept-language"].substring(0,2);
}
if (req.session.language === "de") {
res.locals.TEXTS = TEXTS["de"];
} else {
res.locals.TEXTS = TEXTS["en"];
}
next();
});
// Extract session datastore
app.use((req, res, next) => {
res.locals.superuser = req.session.superuser;
res.locals.username = req.session.username;
res.locals.participantId = req.session.participantId;
res.locals.lastComment = req.session.lastComment;
res.locals.flash = req.session.flash;
res.locals.language = req.session.language;
delete req.session.flash;
next();
});
// ###### Routing ######
const miscRouter = require("./routes/misc");
const eventRouter = require("./routes/event");
const superuserRouter = require("./routes/superuser");
app.use("/", miscRouter);
app.use("/event", eventRouter);
app.use("/superuser", superuserRouter);
// ###### Error handling ######
app.use((err, _req, res, _next) => {
console.log(err);
res.status(404)
.send(err.message);
});
// ###### Let's go! ######
app.listen(PORT, HOST, () => {
console.log(`ThrSqr listening on port ${PORT} of ${HOST}.`);
});