index.js
1.32 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
const express = require('express');
const morgan = require('morgan');
const rfs = require('rotating-file-stream');
const nunjucks = require('nunjucks');
const moment = require('moment');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const routerConfig = require('./router/config');
const app = express();
nunjucks.configure('./webadmin-views', {
autoescape: true,
express: app,
noCache: true,
});
app.use(express.static('./webadmin-statics'));
const accessLogStream = rfs.createStream(
(time, index) => {
const ts = time ? `.${moment(time).format('YYYY-MM-DD')}` : '';
const idx = index ? `.${index}` : '';
return `webadmin.access_log${ts}${idx}`;
},
{
interval: '1d',
path: './logs',
},
);
app.use(morgan('combined', { stream: accessLogStream }));
app.use((req, res, next) => {
res.locals.config = config;
next();
});
app.get('/', (req, res) => {
res.redirect('/config/modem');
});
app.use('/config', routerConfig);
const listenPort = (config.webadmin && config.webadmin.port) || 21923;
app.listen(listenPort, () => {
logger.info(`WEBADMIN listen on port ${listenPort}`);
}).on('error', (err) => {
logger.warn(`WEBADMIN can not start / listen on port ${listenPort}`, { err: err.message });
process.exit();
});