Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hoyce committed Aug 31, 2016
1 parent 911cbf1 commit 9c735d2
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# kth-node-server

Module for initializing an Express server in a Node.js application.

141 changes: 141 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
* <code>serverStartedPromise</code> 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
}
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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://[email protected]:KTH/kth-node-log.git#v1.0.0",
"express": "^4.13.3"
},
"author": "",
"license": "MIT"
}

0 comments on commit 9c735d2

Please sign in to comment.