UNPKG

domotz-remote-pawn

Version:

Domotz Agent

147 lines (124 loc) 4.47 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Iacopo Papalini <ipapalini@domotz.com> on 08/09/15. */ // Starts up an HTTP server that enables the hub setup var createApp = function (fs, http, express, event, bodyParser, favicon, path, routes) { var app = express(); /** * Listening port: if not available, it will be increased until a free one is found * @type {number} */ var port = 3000; var port_file = process.env.DOMOTZ_LISTENER_PORT_FILE; /** * The HTTP server * @type {null} */ var server = null; /** * Resource path */ var viewsPath = path.join(__dirname, '..', '..', 'webapp', 'views'); var faviconPath = path.join(__dirname, '..', '..', 'webapp', 'images', 'favicon.ico'); var webappRootPath = path.join(__dirname, '..', '..', 'webapp'); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-API-Minor-Version, Authorization"); next(); }); // view engine setup app.set('views', viewsPath); app.set('view engine', 'ejs'); app.use(favicon(faviconPath)); app.use(bodyParser.json()); // for parsing application/json app.use(express.static(webappRootPath)); app.use('/', routes); // catch 404 and forward to error handler app.use(function (req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); app.use(function (err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); app.startServing = function () { app.set('port', port); server = http.createServer(app); console.debug("WEBAPP - Try start domotz webapp on port " + port); server.on('error', onError); server.on('listening', onListening); server.listen(port); }; /** * Event listener for HTTP server "listening" event. */ var onListening = function () { var port = app.get('port'); console.info('WEBAPP - Domotz is listening on port ' + port + '... ' + 'Writing port number on ' + port_file); setTimeout(event.onListeningWebapp, 2000); fs.writeFileSync(port_file, port); }; /** * Error handler: starts from * @param error */ var onError = function onError(error) { if (error.syscall !== 'listen') { console.error('Error on System call ' + error.syscall); throw error; } var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': signalCriticalFailureAndExit(bind + ' requires elevated privileges'); break; case 'EADDRINUSE': port += 1; if (port < 65535) { app.startServing(); } else { signalCriticalFailureAndExit('Cannot find a free port to listen to'); } break; default: throw error; } }; /** * Logs a critical error and waits 30s then exit abnormally. * The wait is a cool down period before a possible restart. * @param message */ function signalCriticalFailureAndExit(message) { console.error(message); setTimeout(function () { process.exit(1); }, 30000); } return app; }; module.exports.createApp = createApp;