blob: c132cabcca9aab05ecccd8bfdd80eaaaf9ed45a4 [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(function () {
2 'use strict';
3
4 const express = require('express');
5 const app = express();
6 const config = require('./config/config.js').gateway;
7 const bodyParser = require('body-parser');
8 const cors = require('cors');
9 const socketIo = require('./controllers/websocket.js');
10 const logger = require('./config/logger.js');
11
12 // Apply middlewares
13 app.use(cors());
14 app.use(bodyParser.json());
15
16 // Load routes
17 require('./routes/core_proxy.js')(app);
18
19 app.get('/', function(req, res) {
20 res.send('Hello world');
21 });
22
Matteo Scandolof05d8a62016-12-06 13:36:49 -080023 const startServer = (port) => {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080024
25 // if is running just return it
26 if(app.server) {
27 return app.server;
28 }
29
Matteo Scandolof05d8a62016-12-06 13:36:49 -080030 const server = app.listen(port || config.port, function() {
31 logger.info(`Express is listening to http://localhost:${port || config.port}`);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080032
33 // once server is ready setup WebSocket
34 socketIo.create(server);
35
36 // start redis
37 require('./controllers/redis.js');
38 });
39 app.server = server;
40 return server;
41 };
42
43 const stopServer = () => {
44 if(app.server) {
45 app.server.close();
46 }
47 }
48
49 if(!module.parent) {
50 startServer();
51 }
52
53 module.exports = {
54 app: app,
55 start: startServer,
56 stop: stopServer
57 };
58})();