Last active
June 20, 2016 19:20
-
-
Save mpriour/5147802 to your computer and use it in GitHub Desktop.
OpenShift Restify NodeJS server module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env node | |
// OpenShift NodeJS Restify Server | |
var restify = require('restify'); | |
/** | |
* Define the basic server. | |
*/ | |
exports = (function() { | |
// Scope. | |
var self = this; | |
/* ================================================================ */ | |
/* Helper functions. */ | |
/* ================================================================ */ | |
/** | |
* Set up server IP address and port # using env variables/defaults. | |
*/ | |
self.setupVariables = function() { | |
// Set the environment variables we need. | |
self.ipaddress = process.env.OPENSHIFT_INTERNAL_IP; | |
self.port = process.env.OPENSHIFT_INTERNAL_PORT || 8080; | |
if (typeof self.ipaddress === "undefined") { | |
// Log errors on OpenShift but continue w/ 127.0.0.1 - this | |
// allows us to run/test the app locally. | |
console.warn('No OPENSHIFT_INTERNAL_IP var, using 127.0.0.1'); | |
self.ipaddress = "127.0.0.1"; | |
}; | |
}; | |
/** | |
* terminator === the termination handler | |
* Terminate server on receipt of the specified signal. | |
* @param {string} sig Signal to terminate on. | |
*/ | |
self.terminator = function(sig){ | |
if (typeof sig === "string") { | |
console.log('%s: Received %s - terminating sample app ...', | |
Date(Date.now()), sig); | |
process.exit(1); | |
} | |
console.log('%s: Node server stopped.', Date(Date.now()) ); | |
}; | |
/** | |
* Setup termination handlers (for exit and a list of signals). | |
*/ | |
self.setupTerminationHandlers = function(){ | |
// Process on exit and signals. | |
process.on('exit', function() { self.terminator(); }); | |
// Removed 'SIGPIPE' from the list - bugz 852598. | |
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', | |
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM' | |
].forEach(function(element, index, array) { | |
process.on(element, function() { self.terminator(element); }); | |
}); | |
}; | |
self.setup = function() { | |
self.setupVariables(); | |
self.setupTerminationHandlers(); | |
self.ready = true; | |
} | |
return { | |
setup: self.setup, | |
server: function(){ | |
if(arguments.length>0 || !self.server){ | |
!self.ready && self.setup(); | |
self.server = restify.createServer(arguments); | |
} | |
return self.server; | |
}, | |
start: function() { | |
!self.server && module.exports.server(); | |
// Start the app on the specific interface (and port). | |
self.server.listen(self.port, self.ipaddress, function() { | |
console.log('%s: Node server started on %s:%d ...', | |
Date(Date.now() ), self.ipaddress, self.port); | |
}); | |
} | |
}; | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment