-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
103 lines (83 loc) · 3.43 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// do not edit this file directly -- it was generated by postinstall.js from server.template.js
//
// content of server.template.js
//
var fs = require('fs');
var path = require('path');
// load our module path cache; if this is the first time the app has been run, the cache will be empty
// and it'll be generated once ghost has started (so subsequent startups will use the cache)
require('./server.cache.modulePath');
// load our stat cache; if this if the first time the app has been run, the cache will be empty and
// it'll be generated once ghost has started (so subsequent startups will use the cache)
require('./server.cache.stat');
// load our file cache; this will have been generated by the post-install process
eval(require('zlib').gunzipSync(fs.readFileSync(path.resolve(__dirname, 'server.cache.js.gz'))).toString());
// save the original readFileSync that we'll override with our caching version
var originalReadFileSync = fs.readFileSync;
// caching version of readFileSync that avoids the filesystem if the file is in the cache
function cachedReadFileSync(file, options) {
if (!options || options === 'utf8') {
var fn = file.replace(path.resolve(__dirname, 'node_modules') + path.sep, '');
if (fn.endsWith('.js')) {
fn = fn.substr(0, fn.length - 3);
}
if (s[fn]) {
return s[fn];
};
}
return originalReadFileSync(file, options);
};
// replace standard readFileSync with our caching version
fs.readFileSync = cachedReadFileSync;
// if iisnode is being used, it defines the port we need to use in an environment
// variable; if this variable is defined, we override the config with it otherwise
// the web app won't work correctly
if (process.env.PORT) {
// we do the require in-place here to ensure it comes from the cache
require('ghost/core/server/config').set('server:port', process.env.PORT);
}
// on Windows, Ctrl-C (SIGINT) won't be recognised unless we go via readline
if (process.platform === 'win32') {
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('SIGINT', function () {
process.emit('SIGINT');
});
}
//
// content of ghost\index.js
//
// # Ghost Startup
// Orchestrates the startup of Ghost when run from command line.
var startTime = Date.now(),
debug = require('ghost-ignition').debug('boot:index'),
ghost, express, common, urlService, parentApp;
debug('First requires...');
ghost = require('ghost/core');
debug('Required ghost');
express = require('express');
common = require('ghost/core/server/lib/common');
urlService = require('ghost/core/server/services/url');
parentApp = express();
debug('Initialising Ghost');
ghost().then(function (ghostServer) {
// Mount our Ghost instance on our desired subdirectory path if it exists.
parentApp.use(urlService.utils.getSubdir(), ghostServer.rootApp);
debug('Starting Ghost');
// Let Ghost handle starting our server instance.
return ghostServer.start(parentApp)
.then(function afterStart() {
// generate module path cache (if it already exists this will do nothing)
require('./server.cache.modulePath.generator');
// generate the stat cache (if it already exists this will do nothing)
require('./server.cache.stat.generator');
common.logging.info('Ghost boot', (Date.now() - startTime) / 1000 + 's');
});
}).catch(function (err) {
common.logging.error(err);
setTimeout(() => {
process.exit(-1);
}, 100);
});