index.js 1.32 KB
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();
});