express-init.js
1.89 KB
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
const path = require('path');
const express = require('express');
const session = require('express-session');
const nunjucks = require('nunjucks');
const uniqid = require('uniqid');
const numeral = require('numeral');
const config = require('../../config');
const logger = require('tektrans-logger');
const routers = require('./routers');
// skip if no approriate config
if (!config || !config.control_panel || !config.control_panel.listen_port) {
return;
}
const app = express();
app.locals.config = config;
app.locals.title = config.control_panel.title || config.handler_name || config.username || config.origin;
app.locals.cp_views_dir = path.dirname(__dirname) + '/views';
app.locals.cp_template = app.locals.cp_views_dir + '/template.html';
// session
app.use(session({
secret: config.control_panel.session_secret || uniqid(),
resave: true,
saveUninitialized: false,
name: config.control_panel.session_name || config.handler_name || config.origin
}));
const static_dir = path.dirname(__dirname) + '/views-static';
app.use(express.static(static_dir, {maxAge: 24 * 3600 * 1000}));
// nunjucks environment
let nunjucksEnv = nunjucks.configure('', {
autoescape: true,
express: app,
noCache: config.control_panel.template_no_cache
});
numeral.register('locale', 'id', {
delimiters: {
thousands: '.',
decimal: ','
},
abbreviations: {
thousand: 'ribu',
million: 'juta',
billion: 'miliar',
trillion: 'triliun'
},
currency: {
symbol: 'Rp.'
}
});
numeral.locale('id');
nunjucksEnv.addFilter('numeral', function(num) {
return numeral(num).format();
})
routers.init(app);
// start http server
app.listen(config.control_panel.listen_port, function () {
logger.info('Web control panel started', {listen_port: config.control_panel.listen_port, cp_views_dir: app.locals.cp_views_dir, static_dir: static_dir});
});