This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
85 lines (73 loc) · 3.13 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
#!/env/node
(async () => {
const fs = require('fs');
const path = require("path");
const installer = require("./install/install");
let skipInstall = (process.argv.length > 2 && process.argv.includes("--skipInstall"));
let forceInstall = (process.argv.length > 2 && process.argv.includes("--forceInstall"));
if(!skipInstall && (!await installer.isInstalled() || forceInstall || await installer.shouldUpdate())) {
let run = (forceInstall & 1) || await installer.checkInstall();
if(run) {
console.log(installer.colourText((run === 1 ? "Installing..." : "Updating..."), "blue", ["italic", "bold"]));
await installer.install(forceInstall);
console.log(installer.colourText("Finished " + (run === 1 ? "installing!" : "updating!"), "green", ["italic", "bold"]));
} else {
console.log(installer.colourText("Skipping installer.", "blue", "italic"));
}
}
// Do config
const config = require("./config");
const serverInfo = config.serverInfo;
// Load important modules
const db = require("./db/db");
const express = require("express");
const scetch = require("scetch")();
const morgan = require("morgan");
const nonce = require("nonce-express");
const helmet = require("helmet");
const cors = require("cors");
const session = require("express-session");
const storage = require("./storage/storage");
const websocket = require("./app/routes/tools/websocket");
// Make the app
let app = express();
app.use(morgan('common', config.morgan));
app.use(nonce());
app.use(helmet(config.helmet));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(session({
secret: config.session.secret,
resave: false,
saveUninitialized: false,
store: db.sessionStore,
name: config.session.cookieName
}));
// Start the app
let appPath = path.join(__dirname, "app");
app.set("views", path.join(appPath, "views"));
app.engine("sce", scetch.engine);
app.set("view engine", "sce");
app.use("/assets", express.static(path.join(appPath, "assets")));
app.use(require("./app/routes/routes"));
// Error handling -- scoped to the root layer!
const errRoutes = require("./app/routes/errors");
app.all("/*", errRoutes.notFound);
app.use(errRoutes.handler);
let server = app.listen(serverInfo.port, serverInfo.host, () => {
console.log(`Storage provider: ${storage.provider}`);
console.log(`Server is listening at http://${serverInfo.host}:${serverInfo.port}...`);
if(config.env.isDev) console.log(`Browsersync is probably available at http://${serverInfo.host}:${parseInt(serverInfo.port) + 1}...`);
});
websocket.setup(server);
})().catch(err => {
console.error(err);
process.exit(1);
});
// TODO: process.onExit
// TODO: process.onError
// TODO: Have a server-side state management system (Redis?)
// TODO: We need to error out a task only, not the whole assignment!