forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (80 loc) · 2.82 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
const config = require('./vue/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 ListManager = require('./state/listManager');
require('./supportsTalib');
// broadcast function
const broadcast = data => {
if(_.isEmpty(data))
return;
_.each(
wss.clients,
client => client.send(JSON.stringify(data))
);
}
cache.set('broadcast', broadcast);
// initialize lists and dump into cache
cache.set('imports', new ListManager);
cache.set('gekkos', new ListManager);
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/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/killGekko', require(ROUTE('killGekko')));
router.post('/api/getCandles', require(ROUTE('getCandles')));
// incoming WS:
// wss.on('connection', ws => {
// ws.on('message', _.noop);
// });
app
.use(cors())
.use(serve(WEBROOT + 'vue'))
.use(bodyParser())
.use(require('koa-logger')())
.use(router.routes())
.use(router.allowedMethods());
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
let nodeCommand = _.last(process.argv[1].split('/'));
if(nodeCommand === 'gekko' && !config.headless) {
try {
opn(location);
} catch(e) {}
}
});