-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
41 lines (38 loc) · 1.38 KB
/
app.js
File metadata and controls
41 lines (38 loc) · 1.38 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
'use strict';
const express = require('express'),
app = express();
exports = module.exports = app;
const _ = require('lodash'),
env = require('./env'),
cookieParser = require('cookie-parser'),
logger = require('morgan'),
log = require('debug-logger')('app'),
jwtInit = require('./middleware/jwtInit'),
core = require('./core'),
indexRouter = require('./routes/index');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(jwtInit(env.jwtKey()));
app.use('/', express.static('./public'));
app.use('/api', indexRouter);
app.use((req, res, next) => next(_.set(new Error('File Not Found'), 'statusCode', 404)));
app.use((err, req, res, next) => {
log.error(err);
res.status(_.get(err, 'statusCode', 500)).json({err});
});
core.history.create('load', env.maxLoadHistory(), env.loadPollPeriod());
core.history.create('temp', env.maxTempHistory(), env.tempPollPeriod());
setInterval(() => core.system.loadavg()
.then(([load]) => {
core.history.add('load', load);
core.Websockets.emit('load', {load, history: core.history.get('load')});
})
.catch(log.error), env.loadPollPeriod());
setInterval(() => core.system.temp()
.then(temp => {
core.history.add('temp', temp.celsius);
core.Websockets.emit('temp', {temp, history: core.history.get('temp')});
})
.catch(log.error), env.tempPollPeriod());