Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
First working version 🚀
Browse files Browse the repository at this point in the history
Usage: require('ghost-ignition').className

- Provides 3 classes to use: config, debug & server
- Config may later also contain some default config, so it is a folder rather than a file
  • Loading branch information
ErisDS committed Aug 3, 2016
1 parent acc30f1 commit e33d03c
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ jspm_packages

# Optional REPL history
.node_repl_history

# IDE
.idea/*
*.iml
*.sublime-*
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
config: require('./lib/config'),
debug: require('./lib/debug'),
server: require('./lib/server')
};
15 changes: 15 additions & 0 deletions lib/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var nconf = require('nconf'),
path = require('path');

var defaults = require(path.join(process.cwd(), 'config.example'));

nconf.set('NODE_ENV', process.env.NODE_ENV);

nconf.argv()
.env()
.file({
file: path.join(process.cwd(), 'config.' + process.env.NODE_ENV + '.json')
});

nconf.defaults(defaults);
module.exports = nconf;
7 changes: 7 additions & 0 deletions lib/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var config = require('./config');
var alias = config.get('alias') || process.env.npm_package_name;
var debug = require('debug');

module.exports = function initDebug(name) {
return debug(alias + ':' + name);
}
106 changes: 106 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var config = require('./config');
var debug = require('./debug')('server');
var http = require('http');

var server;

/**
* Get port from nconf
*/

var port = normalizePort(config.get('port'));

var start = function start(app) {
/**
* Create HTTP server.
*/

server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

app.set('port', port);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

return server;
};

/**
* Stop HTTP server.
*/
var stop = function(done) {
try {
server.close(done);
} catch(e) {
/*jshint unused:false*/
}
};

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Server ready');
console.log('Listening on ' + bind + '\n');
}


module.exports.start = start;
module.exports.stop = stop;
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "ghost-ignition",
"version": "0.0.1",
"description": "Basic configuration and tooling shared across applications",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/TryGhost/Ignition.git"
},
"author": "Ghost Foundation",
"license": "MIT",
"bugs": {
"url": "https://github.com/TryGhost/Ignition/issues"
},
"homepage": "https://github.com/TryGhost/Ignition#readme",
"dependencies": {
"debug": "2.2.0",
"express": "4.14.0",
"nconf": "0.8.4"
}
}

0 comments on commit e33d03c

Please sign in to comment.