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
58 lines (46 loc) · 1.39 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
// config somewhere?
const port = 3000;
const relayInterval = 250;
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('./cache');
// broadcast function
const broadcast = data => {
if(_.isEmpty(data))
return;
_.each(
wss.clients,
client => client.send(JSON.stringify(data))
);
}
cache.set('broadcast', broadcast);
const WEBROOT = __dirname + '/';
app.use(cors());
// attach routes
router.post('/api/scan', require(WEBROOT + 'routes/scanDateRange'));
router.post('/api/backtest', require(WEBROOT + 'routes/backtest'));
router.get('/api/strategies', require(WEBROOT + 'routes/strategies'));
// wss.on('connection', ws => {
// ws.on('message', _.noop);
// });
app
.use(serve(WEBROOT + 'vue'))
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());
server.on('request', app.callback());
server.listen(port, () => {
let host = 'http://localhost';
console.log('Serving Gekko UI on ' + host + ':' + server.address().port);
opn(host + ':' + server.address().port);
});