-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (44 loc) · 1.52 KB
/
index.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
'use strict';
if( process.env.NODE_ENV != 'production' && module.parent ) {
global.TEST_MODE = true;
global.facebookUrl = 'http://localhost:4201';
global.firebaseUrl = 'http://localhost:4202';
} else {
global.TEST_MODE = process.env.TEST_MODE
}
const express = require('express');
const bodyParser = require('body-parser');
const log = require('./services/log');
const socket = require('./services/socket');
const api = express();
const app = express();
api.use(bodyParser.json());
// disable 304s
api.disable('etag');
// healthcheck
api.get('/', function(req, res) { res.json({version: 'v1.1'}); });
api.get('/kill/:platform/:version', function(req, res) { return res.sendStatus(404); });
// api routes
const normalizedPath = require("path").join(__dirname, "controllers");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./controllers/" + file)(api);
});
api.use(function(err, req, res, next) {
log.error({err: err, message: err.message, errName: err.name, stack: err.stack}, 'Uncaught server error');
res.status(500).json({message: 'Something went wrong.'});
})
app.use('/', api);
const server = socket.bind(app);
if( process.env.NODE_ENV != 'production' && module.parent ) {
module.exports = function(port) {
const ref = server.listen(port);
const handle = ref.close.bind(ref);
return handle;
}
return;
}
const port = process.env.PORT || 3000
server.listen(port, function(err) {
if( err ) { throw err; }
log.info(`Listening on ${port}`);
});