blob: 01cf1e5e7b235ef437cd75f689c4f81b088eeeff [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080019(function () {
20 'use strict';
21
22 const express = require('express');
23 const app = express();
24 const config = require('./config/config.js').gateway;
25 const bodyParser = require('body-parser');
26 const cors = require('cors');
27 const socketIo = require('./controllers/websocket.js');
28 const logger = require('./config/logger.js');
29
30 // Apply middlewares
31 app.use(cors());
32 app.use(bodyParser.json());
33
34 // Load routes
Matteo Scandolof64cdd52017-02-24 07:52:49 -080035 // require('./routes/core_proxy.js')(app);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080036
37 app.get('/', function(req, res) {
Matteo Scandolof64cdd52017-02-24 07:52:49 -080038 res.send('Welcome to the websocket server for XOS');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080039 });
40
Matteo Scandolof05d8a62016-12-06 13:36:49 -080041 const startServer = (port) => {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080042
43 // if is running just return it
44 if(app.server) {
45 return app.server;
46 }
47
Matteo Scandolof05d8a62016-12-06 13:36:49 -080048 const server = app.listen(port || config.port, function() {
49 logger.info(`Express is listening to http://localhost:${port || config.port}`);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080050
51 // once server is ready setup WebSocket
52 socketIo.create(server);
53
54 // start redis
55 require('./controllers/redis.js');
56 });
57 app.server = server;
58 return server;
59 };
60
61 const stopServer = () => {
62 if(app.server) {
63 app.server.close();
64 }
65 }
66
67 if(!module.parent) {
68 startServer();
69 }
70
71 module.exports = {
72 app: app,
73 start: startServer,
74 stop: stopServer
75 };
76})();