This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,8 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# IDE | ||
.idea/* | ||
*.iml | ||
*.sublime-* |
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
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') | ||
}; |
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
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; |
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
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); | ||
} |
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
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; |
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
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" | ||
} | ||
} |