Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
| /* eslint no-console:0 */ | |
| var loopback = require('loopback'); | |
| var boot = require('loopback-boot'); | |
| var childProcess = require('child_process'); | |
| var async = require('async'); | |
| var path = require('path'); | |
| var debug = require('debug')('sl-nginx-ctl'); | |
| var nginxConf = require('./nginx-conf'); | |
| function setup(baseDir, nginxPath, controlEndpoint, listenEndpoint, nginxRoot) { | |
| var app = loopback(); | |
| app.set('nginxPath', nginxPath); | |
| // Set the IP and port that server should listen on | |
| app.set('host', controlEndpoint.hostname); | |
| app.set('port', controlEndpoint.port); | |
| // Set Nginx listen port | |
| app.set('nginxHost', listenEndpoint.hostname); | |
| app.set('nginxPort', listenEndpoint.port); | |
| app.set('nginxRoot', nginxRoot); | |
| app.set('baseDir', baseDir); | |
| function _reloadNginx(next) { | |
| async.series([ | |
| nginxConf.bind(null, app), | |
| app._nginxCmd.bind(app, 'reload') | |
| ], function(err) { | |
| if (err) return next(err); | |
| app.emit('reloaded'); | |
| console.log( | |
| 'StrongLoop Nginx Controller listening at: http://%s:%s', | |
| app.get('host'), | |
| app.get('port') | |
| ); | |
| next(); | |
| }); | |
| } | |
| app._reloadNginx = _reloadNginx; | |
| function _configureNginx(next) { | |
| nginxConf(app, next); | |
| } | |
| app._configureNginx = _configureNginx; | |
| /** | |
| * start/stop/reload Nginx daemon. | |
| * | |
| * @param action | |
| * @param cb | |
| * @private | |
| */ | |
| function _nginxCmd(action, cb) { | |
| var nginxConfdir = path.join(app.get('baseDir'), 'nginx'); | |
| var configFile = path.join(nginxConfdir, 'nginx.conf'); | |
| var cmd = [app.get('nginxPath'), '-p', nginxConfdir, '-c', configFile]; | |
| if (action !== 'start') cmd.push('-s', action); | |
| debug('CMD: %s', cmd.join(' ')); | |
| childProcess.exec(cmd.join(' '), function(err, stdout, stderr) { | |
| debug(err); | |
| debug('Stdout:\n %s', stdout); | |
| debug('Stderr:\n %s', stderr); | |
| cb(err); | |
| }); | |
| } | |
| app._nginxCmd = _nginxCmd; | |
| function start(cb) { | |
| async.series([ | |
| app.listen.bind(app), | |
| app._configureNginx.bind(app), | |
| app._nginxCmd.bind(app, 'start') | |
| ], function(err) { | |
| if (err) { | |
| if (cb) return cb(err); | |
| console.log('Error starting load-balancer: ', err.message); | |
| throw err; | |
| } | |
| app.emit('started'); | |
| console.log( | |
| 'StrongLoop Nginx Controller listening at: http://%s:%s', | |
| app.get('host'), | |
| app.get('port') | |
| ); | |
| if (cb) return cb(); | |
| }); | |
| } | |
| app.start = start; | |
| function stop(cb) { | |
| app._nginxCmd('stop', cb); | |
| } | |
| app.stop = stop; | |
| stopWhenDone(app); | |
| // Set up the /favicon.ico | |
| app.use(loopback.favicon()); | |
| // request pre-processing middleware | |
| app.use(loopback.compress()); | |
| // -- Add your pre-processing middleware here -- | |
| // boot scripts mount components like REST API | |
| boot(app, __dirname); | |
| // -- Mount static files here-- | |
| // All static middleware should be registered at the end, as all requests | |
| // passing the static middleware are hitting the file system | |
| // Example: | |
| // var path = require('path'); | |
| // app.use(loopback.static(path.resolve(__dirname, '../client'))); | |
| // Requests that get this far won't be handled | |
| // by any middleware. Convert them into a 404 error | |
| // that will be handled later down the chain. | |
| app.use(loopback.urlNotFound()); | |
| // The ultimate error handler. | |
| app.use(loopback.errorHandler()); | |
| return app; | |
| } | |
| function stopWhenDone(app) { | |
| function dieBy(signal) { | |
| console.log('stopped with %s', signal); | |
| app.stop(function(err) { | |
| if (err) { | |
| debug('Error while exiting: ', err); | |
| console.log('An error occurred while stopping. ' + | |
| 'There may be a stale nginx process.'); | |
| } | |
| // re-kill ourself, so our exit status is signaled | |
| process.kill(process.pid, signal); | |
| }); | |
| } | |
| function dieOn(signal) { | |
| process.once(signal, dieBy.bind(null, signal)); | |
| } | |
| dieOn('SIGINT'); | |
| dieOn('SIGTERM'); | |
| process.on('exit', function() { | |
| app.stop(); | |
| }); | |
| process.once('SIGHUP', app._reloadNginx.bind(app)); | |
| } | |
| module.exports = setup; |