-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (63 loc) · 1.81 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
'use strict';
const bodyParser = require('body-parser');
const express = require('express');
const morgan = require('morgan');
const ps = require('ps-node');
const _ = require('lodash');
const LightningError = require('./lightning-error');
const app = express();
app.set('env', process.env.NODE_ENV || 'development');
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// Check if Lightning is running.
ps.lookup({
command: 'lightningd',
psargs: 'ux'
}, (err, resultList) => {
if (err) {
throw new Error(err);
}
if (resultList.length === 0) {
console.log('Lightningd is not running.');
}
resultList.forEach(process => {
if (process) {
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments);
}
});
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan('common'));
app.use('/api/lightning', require('./routes/lightning-api'));
app.use('/api/settings', require('./routes/settings-api'));
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
if (_.isError(err) && !(err instanceof LightningError)) {
let error = 'server_error';
if (req.app.get('env') === 'development') {
error = {
message: err.toString(),
stackTrace: err.stack
};
}
res.status(500).send({error});
return;
}
if (err instanceof LightningError) {
err = err.message;
}
res.status(400).send({error: err});
});
app.use((req, res) => {
return res.status(404).send('404: Not found');
});
app.listen(process.env.PORT || 9000, () => {
console.log(`Listening on port ${process.env.PORT || 9000}`);
});