-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
156 lines (142 loc) · 5.45 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const express = require("express");
const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo")(session);
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const passport = require("passport");
const promisify = require("es6-promisify");
const flash = require("connect-flash");
const expressValidator = require("express-validator");
const routes = require("./routes/index");
const apiRoutesAuthRouter = require("./routes/api/auth");
const apiRoutesParticipants = require("./routes/api/participants");
const apiRoutesNotifications = require("./routes/api/notifications");
const apiRoutesJobs = require("./routes/api/jobs");
const helpers = require("./helpers");
const errorHandlers = require("./handlers/errorHandlers");
require("./handlers/passport");
const language = require("./config/lang");
const authController = require("./controllers/authController");
const userController = require("./controllers/userController");
const paymentController = require("./controllers/paymentController");
const jobController = require("./controllers/jobController");
const crypto = require("crypto");
const Agendash = require("agendash");
const app = express();
app.set("views", path.join(__dirname, "views")); // this is the folder where we keep our pug files
app.set("view engine", "pug"); // we use the engine pug, mustache or EJS work great too
app.use(express.static("public"));
// webhook for payment events with stripe
app.post(
"/payment/webhook",
bodyParser.raw({ type: "*/*" }),
paymentController.webhook
);
app.use(cookieParser());
app.use(bodyParser.json({ limit: "500mb" }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(
session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
cookie: { maxAge: 1000 * 60 * 60 * 24 * 365 }, // 1 year
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
const noncevalue = crypto.randomBytes(20).toString("hex");
res.setHeader(
"Content-Security-Policy",
`worker-src http://localhost https://samply.uni-konstanz.de; script-src https://samply.uni-konstanz.de 'nonce-${noncevalue}' 'unsafe-eval' `
);
// had to enable for API to work
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.locals.noncevalue = noncevalue;
res.locals.h = helpers;
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
res.locals.visitor_language = req.session && req.session.visitor_language;
const path = String(req.path).split("/")[1] || "index";
if (
res.locals.user != null &&
res.locals.user.language &&
language[res.locals.user.language]
) {
res.locals.l =
language[res.locals.user.language][path] || language["english"][path];
res.locals.layout =
language[res.locals.user.language]["layout"] ||
language["english"]["layout"];
res.locals.language = res.locals.user.language.substring(0, 2);
if (res.locals.language == "ge") {
res.locals.language = "de";
}
} else {
if (res.locals.visitor_language) {
const visitor_lang = res.locals.visitor_language;
res.locals.locale_language = visitor_lang || "english";
res.locals.l = language[visitor_lang][path] || language["english"][path];
res.locals.layout =
language[visitor_lang]["layout"] || language["english"]["layout"];
res.locals.language = visitor_lang.substring(0, 2);
if (res.locals.language == "ge") {
res.locals.language = "de";
}
} else {
if (req.headers && req.headers["accept-language"]) {
const lang = req.headers["accept-language"].slice(0, 2);
if (lang == "de") {
res.locals.locale_language = "german";
res.locals.l = language["german"][path];
res.locals.layout = language["german"]["layout"];
res.locals.language = "de";
} else if (lang == "nl") {
res.locals.locale_language = "dutch";
res.locals.l = language["dutch"][path];
res.locals.layout = language["dutch"]["layout"];
res.locals.language = "nl";
} else {
res.locals.locale_language = "english";
res.locals.l = language["english"][path];
res.locals.layout = language["english"]["layout"];
res.locals.language = "en";
}
}
}
}
next();
});
app.use(
"/dash",
authController.isSuperAdminLoggedIn,
Agendash(jobController.agenda)
);
app.use((req, res, next) => {
req.login = promisify(req.login, req);
next();
});
app.use("/", routes);
app.use("/webapi/v1/auth", apiRoutesAuthRouter.router);
app.use("/webapi/v1/participants", apiRoutesParticipants);
app.use("/webapi/v1/notifications", apiRoutesNotifications);
app.use("/webapi/v1/jobs", apiRoutesJobs);
app.use(errorHandlers.notFound);
app.use(errorHandlers.flashValidationErrors);
if (app.get("env") === "development") {
app.use(errorHandlers.developmentErrors);
}
app.use(errorHandlers.productionErrors);
module.exports = app;