mirror of https://github.com/TryGhost/Ghost.git
Make it possible to require ghost as a module. fixes #1326
This commit is contained in:
parent
7a1503cf52
commit
e76d23cb19
|
@ -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,9 +30,24 @@ 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();
|
||||
}
|
||||
|
||||
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 () {
|
||||
|
@ -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.
|
||||
|
@ -147,3 +166,6 @@ when(ghost.init()).then(function () {
|
|||
}, function (err) {
|
||||
errors.logErrorAndExit(err);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = init;
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
12
index.js
12
index.js
|
@ -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();
|
|
@ -22,6 +22,7 @@
|
|||
"url": "https://raw.github.com/TryGhost/Ghost/master/LICENSE"
|
||||
}
|
||||
],
|
||||
"main": "./core/server",
|
||||
"scripts": {
|
||||
"start": "node index",
|
||||
"test": "grunt validate --verbose"
|
||||
|
|
Loading…
Reference in New Issue