// 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);

setInterval(() => {
  console.log('broadcasting..');
  let m = {
    type: 'a',
    payload: Math.random()
  }
  broadcast(m);
}, 5000)

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);
});
@@ -739,145 +739,8 @@
 );%0A%0A
-setInterval(() =%3E %7B%0A  console.log('broadcasting..');%0A  let m = %7B%0A    type: 'a',%0A    payload: Math.random()%0A  %7D%0A  broadcast(m);%0A%7D, 5000)%0A%0A
 cons

remove debug ws messaging