-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.js
81 lines (74 loc) · 2.97 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
const NodeCache = require('node-cache');
const app = require('express')();
const bodyparser = require('body-parser');
let configFile = './cl-rest-config.json';
fs = require('fs');
function prepDataForLogging(msg) {
return typeof msg === 'string' ? msg : JSON.stringify(msg)
}
function configLogger(config) {
if (typeof global.REST_PLUGIN_CONFIG === 'undefined') {
return {
log(msg) { if (config.EXECMODE === 'test') config.PLUGIN.log("info:", prepDataForLogging(msg)) },
warn(msg) { config.PLUGIN.log("warn:", prepDataForLogging(msg)) },
error(msg) { config.PLUGIN.log("error:", prepDataForLogging(msg)) }
}
}
return {
log(msg) { if (config.EXECMODE === 'test') config.PLUGIN.log(prepDataForLogging(msg), "info") },
warn(msg) { config.PLUGIN.log(prepDataForLogging(msg), "warn") },
error(msg) { config.PLUGIN.log(prepDataForLogging(msg), "error") }
}
}
if (typeof global.REST_PLUGIN_CONFIG === 'undefined') {
//Read config file when not running as a plugin
console.log("Reading config file");
let rawconfig = fs.readFileSync(configFile, function (err) {
if (err) {
console.warn("Failed to read config key");
console.error(error);
process.exit(1);
}
});
global.config = JSON.parse(rawconfig);
global.config.PLUGIN = console;
} else {
global.config = global.REST_PLUGIN_CONFIG;
}
global.logger = configLogger(global.config);
global.appCache = new NodeCache({stdTTL: 600});
//LN_PATH is the path containing lightning-rpc file
let lnpath = (global.config.LNRPCPATH && global.config.LNRPCPATH.trim() !== '') ? global.config.LNRPCPATH.trim() : process.env.LN_PATH;
global.ln = require('./lightning-client-js')(lnpath);
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, filePath, macaroon, encodingtype"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});
//Use declared routes here
app.use('/v1/getinfo', require('./routes/getinfo'));
app.use('/v1/utility', require('./routes/getinfo'));
app.use('/v1/newaddr', require('./routes/newaddr'));
app.use('/v1/getBalance', require('./routes/getBalance'));
app.use('/v1/listFunds', require('./routes/listfunds'));
app.use('/v1/getFees', require('./routes/getFees'));
app.use('/v1/withdraw', require('./routes/withdraw'));
app.use('/v1/peer', require('./routes/peers'));
app.use('/v1/channel', require('./routes/channel'));
app.use('/v1/pay', require('./routes/payments'));
app.use('/v1/invoice', require('./routes/invoice'));
app.use('/v1/network', require('./routes/network'));
app.use('/v1/rpc', require('./routes/rpc'));
app.use('/v1/offers', require('./routes/offers'));
app.use('/v1/peerswap', require('./routes/peerswap'));
app.use('/v1/datastore', require('./routes/datastore'));
module.exports = app;