This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
server.js
139 lines (111 loc) · 3.79 KB
/
server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const config = require('./vue/dist/UIconfig');
const koa = require('koa');
const serve = require('koa-static');
const cors = require('koa-cors');
const _ = require('lodash');
const bodyParser = require('koa-bodyparser');
const opn = require('opn');
const server = require('http').createServer();
const router = require('koa-router')();
const ws = require('ws');
const app = koa();
const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({ server: server });
const cache = require('./state/cache');
const nodeCommand = _.last(process.argv[1].split('/'));
const isDevServer = nodeCommand === 'server' || nodeCommand === 'server.js';
wss.on('connection', ws => {
ws.isAlive = true;
ws.on('pong', () => {
ws.isAlive = true;
});
ws.ping(_.noop);
ws.on('error', e => {
console.error(new Date, '[WS] connection error:', e);
});
});
setInterval(() => {
wss.clients.forEach(ws => {
if(!ws.isAlive) {
console.log(new Date, '[WS] stale websocket client, terminiating..');
return ws.terminate();
}
ws.isAlive = false;
ws.ping(_.noop);
});
}, 10 * 1000);
// broadcast function
const broadcast = data => {
if(_.isEmpty(data)) {
return;
}
const payload = JSON.stringify(data);
wss.clients.forEach(ws => {
ws.send(payload, err => {
if(err) {
console.log(new Date, '[WS] unable to send data to client:', err);
}
});
}
);
}
cache.set('broadcast', broadcast);
const ListManager = require('./state/listManager');
const GekkoManager = require('./state/gekkoManager');
// initialize lists and dump into cache
cache.set('imports', new ListManager);
cache.set('gekkos', new GekkoManager);
cache.set('apiKeyManager', require('./apiKeyManager'));
// setup API routes
const WEBROOT = __dirname + '/';
const ROUTE = n => WEBROOT + 'routes/' + n;
// attach routes
const apiKeys = require(ROUTE('apiKeys'));
router.get('/api/info', require(ROUTE('info')));
router.get('/api/strategies', require(ROUTE('strategies')));
router.get('/api/configPart/:part', require(ROUTE('configPart')));
router.get('/api/apiKeys', apiKeys.get);
const listWraper = require(ROUTE('list'));
router.get('/api/imports', listWraper('imports'));
router.get('/api/gekkos', listWraper('gekkos'));
router.get('/api/exchanges', require(ROUTE('exchanges')));
router.post('/api/addApiKey', apiKeys.add);
router.post('/api/removeApiKey', apiKeys.remove);
router.post('/api/scan', require(ROUTE('scanDateRange')));
router.post('/api/scansets', require(ROUTE('scanDatasets')));
router.post('/api/backtest', require(ROUTE('backtest')));
router.post('/api/import', require(ROUTE('import')));
router.post('/api/startGekko', require(ROUTE('startGekko')));
router.post('/api/stopGekko', require(ROUTE('stopGekko')));
router.post('/api/deleteGekko', require(ROUTE('deleteGekko')));
router.post('/api/getCandles', require(ROUTE('getCandles')));
// incoming WS:
// wss.on('connection', ws => {
// ws.on('message', _.noop);
// });
app
.use(cors())
.use(serve(WEBROOT + 'vue/dist'))
.use(bodyParser())
.use(require('koa-logger')())
.use(router.routes())
.use(router.allowedMethods());
server.timeout = config.api.timeout || 120000;
server.on('request', app.callback());
server.listen(config.api.port, config.api.host, '::', () => {
const host = `${config.ui.host}:${config.ui.port}${config.ui.path}`;
if(config.ui.ssl) {
var location = `https://${host}`;
} else {
var location = `http://${host}`;
}
console.log('Serving Gekko UI on ' + location + '\n');
// only open a browser when running `node gekko`
// this prevents opening the browser during development
if(!isDevServer && !config.headless) {
opn(location)
.catch(err => {
console.log('Something went wrong when trying to open your web browser. UI is running on ' + location + '.');
});
}
});