-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
89 lines (70 loc) · 2.2 KB
/
app.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
var express = require('express'),
reqlib = require('app-root-path').require,
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
app = express(),
jsonStore = reqlib('/lib/jsonStore.js'),
store = reqlib('config/store.js'),
cors = require('cors'),
config = reqlib('config/app.js'),
debug = reqlib('/lib/debug')('App'),
broker = reqlib('lib/broker'),
history = require('connect-history-api-fallback'),
utils = reqlib('/lib/utils.js');
debug("Application path:" + utils.getPath(true));
// view engine setup
app.set('views', utils.joinPath(utils.getPath(), 'views'));
app.set('view engine', 'ejs');
app.use(history());
app.use(logger('dev'));
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
app.use(cookieParser());
app.use('/', express.static(utils.joinPath(utils.getPath(), 'dist')));
app.use(cors());
// ----- APIs ------
// get settings
app.get('/api/settings', function(req, res) {
res.json({success:true, settings: jsonStore.get(store.settings)});
})
// update settings
app.post('/api/settings', function(req, res) {
jsonStore.put(store.settings, req.body)
.then(data => {
broker.restart();
res.json({success: true, message: "Configuration updated successfully"})
})
.catch(err => {
debug(err)
res.json({success: false, message: err.message})
})
})
// get settings
app.get('/api/status', function(req, res) {
res.json({success:true, status: broker.status()});
})
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
console.log("%s %s %d - Error: %s", req.method, req.url, err.status, err.message);
// render the error page
res.status(err.status || 500);
res.redirect('/');
});
// PROCESS MANAGEMENT
process.on('SIGINT', function() {
debug('Closing...');
process.exit();
});
broker.init();
//setTimeout(broker.init, 5000)
module.exports = app;