-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
157 lines (128 loc) · 4.64 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
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
const bodyParser = require("body-parser")
const express = require("express")
const Sentry = require("@sentry/node")
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 dashboardController = require("./controllers/dashboardController")
const format = require("./lib/format")
const createConfController = require("./controllers/createConfController")
const landingController = require("./controllers/landingController")
const userController = require("./controllers/userController")
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()
Sentry.init({
dsn: process.env.SENTRY_DSN
})
app.use(Sentry.Handlers.requestHandler())
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
app.locals.format = format
app.use("/static", express.static("static"))
app.use("/static/gouvfr", express.static(path.join(__dirname, "node_modules/@gouvfr/dsfr/dist")))
app.use("/chart.js", express.static(path.join(__dirname, "node_modules/chart.js/dist")))
app.use("/static/remixicon", express.static(path.join(__dirname, "node_modules/remixicon/fonts")))
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
res.locals.siteUrl = config.HOSTNAME_WITH_PROTOCOL
res.locals.user = req.session.user
next()
})
app.get(urls.landing, landingController.getLanding)
app.post(urls.startAuth, userController.startAuth)
app.post(urls.logout, userController.logout)
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",
})
})
app.get(urls.dashboard, dashboardController.getDashboard)
app.post(urls.participantAction, dashboardController.participantAction)
app.post(urls.fetchDashboardInfo, dashboardController.fetchDashboardInfo)
if (config.FEATURE_STATS_PAGE) {
app.get(urls.stats, async (req, res) => {
if (config.STATS_EXTERNAL_DASHBOARD_URL) {
res.render("stats", {
pageTitle: "Statistiques",
dashboardUrl: config.STATS_EXTERNAL_DASHBOARD_URL,
})
return
}
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.faq, (req, res) => {
res.render("faq", {
pageTitle: "Questions fréquentes",
})
})
app.get(urls.logout, userController.logout)
app.get(urls.status, statusController.getStatus)
app.use(Sentry.Handlers.errorHandler())
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)
})