Make it possible to require ghost as a module. fixes #1326

This commit is contained in:
Harry Wolff 2013-11-19 21:43:55 -05:00
parent 7a1503cf52
commit e76d23cb19
4 changed files with 175 additions and 129 deletions

View File

@ -1,5 +1,11 @@
// If no env is set, default to development
// This needs to be above all other require()
// modules to ensure config gets right setting.
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// Module dependencies
var express = require('express'),
var configLoader = require('./config-loader.js'),
express = require('express'),
when = require('when'),
_ = require('underscore'),
semver = require('semver'),
@ -14,9 +20,9 @@ var express = require('express'),
packageInfo = require('../package.json'),
// Variables
loading = when.defer(),
server = express(),
ghost = new Ghost();
ghost = new Ghost(),
init,
setup;
// If we're in development mode, require "when/console/monitor"
// for help in seeing swallowed promise errors.
@ -24,12 +30,27 @@ if (process.env.NODE_ENV === 'development') {
require('when/monitor/console');
}
// Expose the promise we will resolve after our pre-loading
ghost.loaded = loading.promise;
// Initializes the ghost application.
function init(app) {
if (!app) {
app = express();
}
when(ghost.init()).then(function () {
configLoader.loadConfig().then(function () {
// The server and its dependencies require a populated config
setup(app);
}).otherwise(errors.logAndThrowError);
}
// Sets up the express server instance.
// Instantiates the ghost singleton,
// helpers, routes, middleware, and plugins.
// Finally it starts the http server.
function setup(server) {
when(ghost.init()).then(function () {
return helpers.loadCoreHelpers(ghost);
}).then(function () {
}).then(function () {
// ##Configuration
// set the view engine
@ -113,8 +134,6 @@ when(ghost.init()).then(function () {
});
}
// Let everyone know we have finished loading
loading.resolve();
}
// Expose the express server on the ghost instance.
@ -144,6 +163,9 @@ when(ghost.init()).then(function () {
}
});
}, function (err) {
}, function (err) {
errors.logErrorAndExit(err);
});
});
}
module.exports = init;

View File

@ -0,0 +1,29 @@
/*globals describe, beforeEach, it*/
var net = require('net'),
assert = require('assert'),
should = require('should'),
request = require('request'),
server = require('../../server'),
Ghost = require('../../ghost'),
config = require('../../../config'),
ghost = new Ghost();
describe('Server', function () {
var port = config['development'].server.port,
host = config['development'].server.host,
url = 'http://' + host + ':' + port;
it('should not start a connect server when required', function (done) {
request(url, function (error, response, body) {
assert.equal(response, undefined);
assert.equal(body, undefined);
assert.notEqual(error, undefined);
assert.equal(error.code, 'ECONNREFUSED');
done();
});
});
});

View File

@ -1,13 +1,7 @@
// # Ghost bootloader
// Orchestrates the loading of Ghost
// When run from command line.
var configLoader = require('./core/config-loader.js'),
error = require('./core/server/errorHandling');
var ghost = require('./core/server');
// If no env is set, default to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
configLoader.loadConfig().then(function () {
// The server and its dependencies require a populated config
require('./core/server');
}).otherwise(error.logAndThrowError);
ghost();

View File

@ -22,6 +22,7 @@
"url": "https://raw.github.com/TryGhost/Ghost/master/LICENSE"
}
],
"main": "./core/server",
"scripts": {
"start": "node index",
"test": "grunt validate --verbose"