-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (82 loc) · 2.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @autor Kristof Friess
* @company Zebresel
* @copyright Since 2018 by Zebresel - Deine Agentur für digitale Medien
* @description This is the main controller which initialized the APM Webpage and API
*/
/*jshint esversion: 6 */
/*jshint node: true*/
// CLASSES
const RouteManager = require('./core/routeManager.js');
const Sequelize = require('sequelize');
// Variables
const os = require('os');
const path = require('path');
const express = require('express');
const app = express();
const http = require('http').Server(app);
const expressLess = require('express-less');
const env = require('node-env-file');
const fs = require('fs');
const i18n = require('i18n');
const session = require('express-session');
const fileStore = require('session-file-store')(session);
// Load another ENV file - and overwrite any defined ENV variables.
if(fs.existsSync(__dirname + '/.env'))
{
env(__dirname + '/.env', {overwrite: true});
}
// setup i18n
i18n.configure({
locales:['en'],
directory: __dirname + '/messages',
api: {
'__': 'l', //now req.__ becomes req.t
'__n': 'ln' //and req.__n can be called as req.tn
}
});
// Config should be load after env, because of overwrites
app.config = require('./config/config.js');
// remove express powered by header
app.disable('x-powered-by');
// add session handling
app.use(session({
store: new fileStore({
path: path.join(os.tmpdir(), 'simple-apm', 'sessions')
}),
secret: app.config.sessionSecret
}));
// static routes
app.use('/assets', express.static('assets'));
// initial setup for css
app.use('/assets/css', expressLess(__dirname + '/src/less', { compress: true }));
// combine express and i18n
app.use(i18n.init);
// setup database
app.sequelize = new Sequelize(app.config.db.name, app.config.db.user, app.config.db.password, {
host: app.config.db.host,
dialect: app.config.db.dialect,
logging: true, // SQL Logging
pool: {
max: 5,
min: 0,
idle: 10000
},
// Used for SQLite only
// storage: app.config.db.path // like 'path/to/database.sqlite'
});
// load all models to app
app.models = {};
const modelDir = './models';
const modelFiles = fs.readdirSync(modelDir);
for (let i = modelFiles.length - 1; i >= 0; i--)
{
let className = modelFiles[i].charAt(0).toUpperCase() + modelFiles[i].substr(1, modelFiles[i].length - 4);
app.models[className] = require(modelDir + '/' + modelFiles[i])(app.sequelize);
}
// init route manager
const router = new RouteManager(app, require('./config/routes.js'))
// start simple server to read
http.listen(app.config.port, function() {
console.log('listening on *:' + this.address().port);
});