-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
127 lines (97 loc) · 4.02 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const express = require('express');
const cookieParser = require('cookie-parser');
const MongoStore = require('connect-mongo');
const chalk = require('chalk');
const session = require('express-session');
const fs = require('fs');
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
require('./src/server/models/vrsModel'); // created model loading here
const fileUpload = require('express-fileupload');
const pino = require('pino');
const path = require('path');
const compression = require('compression');
const passport = require('passport');
const User = mongoose.model('VRSUser');
const LocalStrategy = require('passport-local').Strategy;
const logger = require('pino-http')({
name: 'vrs', autoLogging: true, useLevel: 'info',
}, pino.destination('./application.log'));
const { AppSettings } = require('./lib/AppSettings');
global.AppSettings = new AppSettings();
const { config } = require('./config');
const { Logger } = require('./lib/logger');
const { disableCors } = require('./src/server/middlewares/disableCors');
global.log = new Logger({ dbConnectionString: config.connectionString });
this.logMeta = { scope: 'entrypoint' };
log.info('Init the application', this);
function compressionFilter(req, res) {
if (req.headers['x-no-compression']) {
return false;
}
return compression.filter(req, res);
}
app.use(compression({ filter: compressionFilter }));
app.use(disableCors);
const expressSession = session({
secret: process.env.SYNGRISI_SESSION_STORE_KEY || require('crypto')
.randomBytes(64)
.toString('hex'), resave: true, saveUninitialized: false, cookie: { secure: false },
store: MongoStore.create({ mongoUrl: config.connectionString }),
});
app.use(expressSession);
log.info('Init passport', this);
app.use(passport.initialize());
app.use(session({
store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-app' }),
}));
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(logger);
app.use(cookieParser());
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));
log.info('Connect to database', this);
// mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect(config.connectionString, { useUnifiedTopology: true });
const viewPath = path.join(__dirname, 'mvc/views');
app.set('views', viewPath);
app.set('view engine', 'ejs');
app.use(express.json({ limit: '50mb' }));
app.use('/snapshoots', express.static(config.defaultBaselinePath));
app.use('/static', express.static('./static'));
app.use('/assets', express.static('./mvc/views/react/assets'));
app.use('/lib', express.static('./mvc/views/lib'));
const routes = require('./src/server/routes/v1');
app.use('/v1', routes);
app.use('/auth', require('./src/server/routes/ui/auth'));
app.use('/admin*', require('./src/server/routes/ui/admin'));
app.use('/', require('./src/server/routes/ui'));
app.use((req, res) => {
res.status(404)
.json({ url: `${req.originalUrl} not found` });
});
app.listen(config.port, async () => {
log.debug('run onStart jobs', this);
const startUp = await require('./lib/onStart');
startUp.createTempDir();
await startUp.createBasicUsers();
await startUp.createInitialSettings();
if (process.env.SYNGRISI_TEST_MODE === '1') await startUp.createTestsUsers();
});
log.info('Get Application version', this);
global.version = require('./package.json').version;
log.info('Load devices list', this);
global.devices = require('./src/server/data/devices.json');
if (fs.existsSync('./src/data/custom_devices.json')) {
global.devices = [...global.devices, ...require('./src/server/data/custom_devices.json')];
}
log.info(chalk.green(`Syngrisi version: ${global.version} started at http://localhost:${config.port}`), this);
log.info(chalk.whiteBright('Press <Ctrl+C> to exit'), this);