From 9c735d2fcb6008244fcc169ab63c920c7e58f958 Mon Sep 17 00:00:00 2001 From: hoyce mac work Date: Wed, 31 Aug 2016 17:50:29 +0200 Subject: [PATCH] Initial commit --- .gitignore | 23 +++++++++ README.md | 2 + index.js | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 19 +++++++ 4 files changed, 185 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fae762b --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +.DS_Store + +# IDEA specific +.idea +*.iml + +# VS Code specific +.vscode +jsconfig.json +typings/* +typings.json + +# Eclipse specific +.jshintrc +.project +.settings/ + +# Logs +logs/* +*.log + +# Dependency directory for NPM +node_modules diff --git a/README.md b/README.md index 998f417..9eee363 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # kth-node-server + Module for initializing an Express server in a Node.js application. + diff --git a/index.js b/index.js new file mode 100644 index 0000000..6f3d5a0 --- /dev/null +++ b/index.js @@ -0,0 +1,141 @@ +'use strict' + +var fs = require('fs') +var express = require('express') +var server = express() +var path = require('path') +var httpServer = require('https') + +let config, initCallback, log + +/** + * Initialize the server. Performed before startup. + */ +function _init () { + initCallback() +} + +/** + * Start the server. + */ +var myHttpServer = null +function _start () { + if (config.full.useSsl) { + var options + + if (config.full.ssl.passphrase && config.full.ssl.pfx) { + var password = fs.readFileSync(config.full.ssl.passphrase) + '' + password = password.trim() + log.info('Setting key for HTTPS(pfx): ' + config.full.ssl.pfx) + options = { + pfx: fs.readFileSync(config.full.ssl.pfx), + passphrase: password + } + } else { + options = { + key: fs.readFileSync(config.full.ssl.key), + cert: fs.readFileSync(config.full.ssl.cert), + ca: fs.readFileSync(config.full.ssl.ca) + } + log.info('Setting key for HTTPS(cert): ' + config.full.ssl.cert) + } + + log.info('Secure(HTTPS) server started, listening at ' + config.full.port) + myHttpServer = httpServer.createServer(options, server).listen(config.full.port) + } else { + log.info('Server started, listening at ' + config.full.port) + server.listen(config.full.port) + } + + /** + * Close event of the server + */ + server.on('close', function () { + log.info('Server received close event') + }) + + /** + * Closing the server + * @param signal the log message depending on the given signal. + */ + function serverClose (signal) { + log.info('Process received ' + signal + ', exiting ....') + if (myHttpServer) { + myHttpServer.close(function () { + process.exit(0) + }) + } else { + process.exit(0) + } + } + +// close down gracefully on sigterm, sighup and sigint + process.on('SIGTERM', function () { + serverClose('SIGTERM') + }) + + process.on('SIGHUP', function () { + serverClose('SIGHUP') + }) + + process.on('SIGINT', function () { + serverClose('SIGINT') + }) + + /** + * A list of promises which listen to different + * events in the application which need to resolved + * until the server is fully started. + * + * By adding a new promise in the list below the + * serverStartedPromise will wait + * for that promise (and the rest of the promises + * in the array) to resolve. + */ + var serverStartedPromise = Promise.all([ + createEventPromise('event:strategyInitialized') + ]) + + serverStartedPromise.then(function () { + server.emit('event:serverStarted', 'true') + }) +} + +/** + * Create an Promise which listen for a event. + * + * @param eventName - for the event that the promise will listen for + * @returns {Promise} A Promise which will resolve when the event fires + */ +function createEventPromise (eventName) { + return new Promise( + function (resolve, reject) { + server.on(eventName, function (initialized) { + if (initialized) { + resolve() + } else { + reject() + } + }) + } + ) +} + +/** + * Expose the server and start and init methods. + */ +module.exports = server +module.exports.init = _init +module.exports.start = _start + +module.exports.setConfig = function (_conf) { + config = _conf +} + +module.exports.setInitCallback = function (_cb) { + initCallback = _cb +} + +module.exports.setLog = function (_log) { + log = _log +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4fa3aba --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "kth-node-server", + "version": "1.0.0", + "description": "Module for initializing an Express server in a Node.js application.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/KTH/kth-node-server" + }, + "dependencies": { + "kth-node-log": "git+ssh://git@github.com:KTH/kth-node-log.git#v1.0.0", + "express": "^4.13.3" + }, + "author": "", + "license": "MIT" +}