forked from phihag/bts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.js
115 lines (95 loc) · 2.96 KB
/
ticker.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
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
const async = require('async');
const ws_module = require('ws');
const express = require('express');
const favicon = require('serve-favicon');
const serror = require('../bts/serror');
const utils = require('../bts/utils');
const wshandler = require('../bts/wshandler');
const tdata = require('./tdata');
const tdatabase = require('./tdatabase');
const tget = require('./tget');
const tupdate = require('./tupdate');
const tweb = require('./tweb');
function read_config(callback, autocreate) {
fs.readFile('ticker_config.json', 'utf8', (err, config_json) => {
if (autocreate && err && (err.code === 'ENOENT')) {
fs.readFile('ticker_config.json.default', 'utf8', (err, config_json) => {
if (err) return callback(err);
const config = JSON.parse(config_json);
config.password = utils.gen_token();
fs.writeFile('ticker_config.json', JSON.stringify(config, null, '\t'), 'utf8', (err) => {
if (err) return callback(err);
console.log('Created default configuration in ' + path.resolve('ticker_config.json')); // eslint-disable-line no-console
read_config(callback, false);
});
});
return;
}
if (err) return callback(err);
const config = JSON.parse(config_json);
callback(err, config);
});
}
function main() {
async.waterfall([
cb => read_config(cb, true),
function(config, cb) {
serror.setup(config);
tdatabase.init((err, db) => cb(err, config, db));
},
function (config, db, cb) {
const app = create_app(config, db);
cb(null, app);
},
(app, cb) => tdata.recalc(app, cb),
], function(err) {
if (err) throw err;
});
}
function create_app(config, db) {
const server = require('http').createServer();
const app = express();
const wss = new ws_module.Server({server: server});
app.config = config;
app.db = db;
app.wss = wss;
app.use('/bupdev/', express.static(path.join(utils.root_dir(), 'static/bup/dev/')));
app.use('/static/', express.static('static/', {}));
app.get('/', tweb.main_handler);
app.use(favicon(utils.root_dir() + '/static/icons/favicon.ico'));
app.get('/qjson', tweb.qjson_handler);
wss.on('connection', function connection(ws, req) {
const location = url.parse(req.url, true);
if (location.path === '/ws/ticker') {
return wshandler.handle(tget, app, ws);
} else if (/^\/ws\/update/.test(location.path)) {
const password = location.query.password;
if (password && (password !== app.config.password)) {
try {
ws.send(JSON.stringify({
type: 'error',
message: 'Incorrect password',
}));
} catch(e) {
serror.silent('failed to send password failure');
}
return;
}
return wshandler.handle(tupdate, app, ws);
} else {
ws.send(JSON.stringify({
type: 'error',
message: 'Unsupported location ' + location.path,
}));
ws.close();
}
});
server.on('request', app);
server.listen(config.port, function () {});
return app;
}
main();