forked from numerique-gouv/audioconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (101 loc) · 3.63 KB
/
index.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
const bodyParser = require("body-parser")
const express = require("express")
const flash = require("connect-flash")
const path = require("path")
const cookieParser = require("cookie-parser")
const session = require("express-session")
const MemoryStore = require("memorystore")(session)
const config = require("./config")
const db = require("./lib/db")
const format = require("./lib/format")
const createConfController = require("./controllers/createConfController")
const landingController = require("./controllers/landingController")
const sendValidationEmailController = require("./controllers/sendValidationEmailController")
const statusController = require("./controllers/statusController")
const stats = require("./lib/stats")
const urls = require("./urls")
if (config.NODE_ENV === "development") {
console.dir({ config })
}
const version = require("./package.json").version
const app = express()
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.locals.format = format
app.use("/static", express.static("static"))
// Hack for importing css from npm package
app.use("/~", express.static(path.join(__dirname, "node_modules")))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser(config.secret))
// Only used for Flash not safe for others purposes
app.use(session({
secret: config.SECRET,
resave: false,
saveUninitialized: false, // "complying with laws that require permission before setting a cookie"
cookie: {
maxAge: 300000,
sameSite: "lax" // todo strict would be better for prod
},
store: new MemoryStore({
checkPeriod: 300000
})
}))
app.use(flash())
// Populate some variables for all views
app.use(function(req, res, next){
res.locals.appName = config.APP_NAME
res.locals.supportEmail = config.SUPPORT_EMAIL
res.locals.pollUrl = config.POLL_URL
res.locals.numPinDigits = config.NUM_PIN_DIGITS
res.locals.errors = req.flash("error")
res.locals.infos = req.flash("info")
res.locals.successes = req.flash("success")
res.locals.urls = urls
res.locals.version = version
next()
})
app.get(urls.landing, landingController.getLanding)
app.post(urls.sendValidationEmail, sendValidationEmailController.sendValidationEmail)
app.get(urls.validationEmailSent, (req, res) => {
res.render("validationEmailSent", {
pageTitle: "Un email de validation a été envoyé",
email: req.query.email
})
})
app.get(urls.createConf, createConfController.createConf)
app.get(urls.showConf, createConfController.showConf)
app.post(urls.cancelConf, createConfController.cancelConf)
app.get(urls.legalNotice, (req, res) => {
res.render("legalNotice", {
pageTitle: "Mentions Légales",
})
})
if (config.FEATURE_STATS_PAGE) {
app.get(urls.stats, async (req, res) => {
const NUM_STATS_POINTS = 1440 // 24h if 1 point per hour
let latestStats = []
try {
latestStats = await db.getLatestStatsPoints(NUM_STATS_POINTS)
} catch (err) {
console.error("Impossible de récupérer les statsPoints", err)
}
const formattedStats = stats.formatDataForDisplay(latestStats)
res.render("stats", {
pageTitle: "Statistiques",
stats: formattedStats,
})
})
}
app.get(urls.contact, (req, res) => {
res.render("contact", {
pageTitle: "Contact",
})
})
app.get(urls.status, statusController.getStatus)
module.exports = app.listen(config.PORT, () => {
console.log(`It is ${format.formatFrenchDateTime(new Date())}, ${config.APP_NAME} listening at http://localhost:${config.PORT}`)
})
process.on("unhandledRejection", error => {
// Will print "unhandledRejection err is not defined"
console.log("unhandledRejection erreur : ", error.message)
})