-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
104 lines (95 loc) · 2.74 KB
/
server.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
const express = require("express");
const logger = require("../lib/log")(__filename);
const util = require("../lib/util");
const dbModule = require("../sequelize/db");
const session = require("express-session");
const pgModule = require("../database/postgres/pg");
const {
resetPasswordEmail,
createUser,
deleteUser,
loginUser,
logoutUser,
userResetPassword,
createDatabase,
} = require("./routes/userRoutes");
const { database } = require("./routes/renderRoutes");
require("dotenv").config();
let server = null;
let app = null;
const arangoModule = require("../database/arango/arango");
let cleaner = null;
const getApp = () => {
return app;
};
const startServer = async (portNumber) => {
await dbModule.start();
await pgModule.startPGDB();
await arangoModule.startArangoDB();
cleaner = await util.cleanAnonymous();
return new Promise((resolve, reject) => {
app = express();
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.static("public"));
app.use(
session({
secret: process.env.SECRET,
resave: true,
saveUninitialized: false,
rolling: true,
cookie: {
secure: false,
maxAge: 1000 * 60 * 60 * 24 * 7,
},
})
);
app.set("view engine", "ejs");
app.use(express.json());
app.get("/", (req, res) => {
res.render("welcome", { email: req.session.email });
});
app.get("/signin", (req, res) => {
res.render("signin", { email: req.session.email });
});
app.get("/signup", (req, res) => {
res.render("signup", { email: req.session.email });
});
app.get("/setPassword/:token", (req, res) => {
res.render("setPassword", { email: req.session.email });
});
app.get("/resetPassword", (req, res) => {
res.render("resetPassword", { email: req.session.email });
});
app.get("/tutorial/:database", database);
app.post("/api/notifications", resetPasswordEmail);
app.post("/api/users", createUser);
app.delete("/api/users/:id", deleteUser);
app.post("/api/session", loginUser);
app.delete("/api/session", logoutUser);
app.post("/api/passwordReset", userResetPassword);
app.post("/api/createDatabase/:database", createDatabase);
server = app.listen(portNumber, () => {
resolve(app);
logger.info(`Listening on portNumber ${portNumber}`);
});
});
};
const stopServer = () => {
return new Promise((resolve, reject) => {
cleaner.stop();
dbModule.close();
pgModule.closePGDB();
arangoModule.closeArangoDB();
logger.info("DB has been closed");
server.close(() => {
logger.info("The server has been closed");
resolve();
});
});
};
module.exports = {
startServer,
stopServer,
getApp,
};