-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
79 lines (69 loc) · 1.82 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
'use strict';
/**
* Imports
*/
const express = require('express');
const app = express();
const helmet = require('helmet');
const { existsSync, mkdirSync } = require('fs');
const path = require('path');
const { promisify } = require('util');
const { port, uploadpath, users } = require('./config');
const logger = require('./core/logger');
// const { resolve } = require('path');
const { NODE_ENV: env } = process.env;
const uploads = require('./routes/router');
// const exphbs = require('express-handlebars');
const isProduction = env === 'production';
/**
* Middlewares and inits
*/
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
helmet.contentSecurityPolicy({
directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
'script-src': ["'self'", "'unsafe-inline'"]
}
})
);
express.response.sendFile = promisify(express.response.sendFile);
if (isProduction) {
const job = require('./core/cron');
job.start();
logger.info('Cronjob starting in production mode');
}
/**
* Router
*/
if (isProduction) {
app.use(express.static('./client/build'));
}
app.use('/', uploads);
app.get('/', (req, res) => {
res.render('index');
});
app.listen(port, () =>
// eslint-disable-next-line
console.log(`Server started at port ${port}`)
);
// bootstrap folders/files etc.
(() => {
if (!existsSync(uploadpath)) {
mkdirSync(uploadpath);
}
const usernames = users.reduce((acc, curr) => [...acc, curr[0]], []);
for (const u of usernames) {
const uniquePath = path.resolve(uploadpath, u);
if (!existsSync(uniquePath)) {
mkdirSync(uniquePath);
}
}
})();
process.on('unhandledRejection', e => {
logger.error(e);
});
process.on('uncaughtException', e => {
logger.error(e);
});