-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
183 lines (150 loc) · 4.66 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//if (!module.parent) process.on('uncaughtException', function(err, next) {
//var msg;
//if (err instanceof Error) {
//msg = '[err]: ' + err + '\n' + err.stack;
//} else {
//msg = (err.name || err.reason || err.message);
//console.error(err);
//}
//console.error(msg);
//next && next();
//});
var fs = require('fs');
var central = require(__dirname + '/lib/central.js');
var conf = central.conf;
var express = central.lib.express;
var autostatic = central.autostatic;
var vhosts = [];
// boot specific server
function bootServer(hostname, port, app, cb) {
var conf = central.conf;
var _dir = __dirname + '/servers/' + hostname;
var serverConf = require(_dir);
var server = express();
var cb;
var routes;
try {
routes = require(__dirname + '/servers/' + hostname + '/routes');
} catch (e) {}
server._dir = _dir;
server.hostname = hostname;
server.host = hostname + '.' + central.rootDomain + ':' + (port || conf.port);
server.port = port;
// do something before boot
if ('initSets' in conf) {
conf.initSets(server, express);
}
// give this server a dedicated port
if (port && !cb) {
cb = function(server) {
server.listen(port, '127.0.0.1');
server.log('info', 'listen '.yellow + 'http://127.0.0.1:' + ('' + port).yellow);
};
}
bootApp(server, cb);
serverConf.before && serverConf.before(server, app);
var mdls = serverConf.modules;
if (mdls) mdls.forEach(function(name) { server.addModule(name); });
serverConf.after && serverConf.after(server, app);
server.ending = serverConf.ending;
// init routes...
if (routes) {
server.log('info', 'Routing..');
routes(central, server, app);
}
if ('afterBoot' in conf) {
conf.afterBoot(server, express);
}
var vhost = serverConf.vhost;
if (vhost || !port) {
hostname = (typeof vhost == 'string' && vhost) ? vhost : hostname;
vhosts.push([hostname, server]);
}
return server;
}
// boot application
function bootApp(app, next) {
var passport = central.lib.passport;
app.set('environment', conf.NODE_ENV);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.enable('trust proxy')
app.log = central.utils.applog;
app.addModule = central.addModule;
app.extendModule = central.extendModule;
app.modules = {};
app.mount = central.modules.__proto__.mount;
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.cookieSession({
secret: central.conf.salt,
cookie: { domain: '.' + central.rootDomain },
store: central.sessionStore
}));
app.use(express.bodyParser());
app.use(express.csrf());
app.use(passport.initialize());
app.use(passport.session());
central.reqbase.addHelpers(app);
app.use(app.router);
if (app.hostname === 'www') {
app.engine('less', function(path, options, fn) {
console.log(arguments);
fs.readFile(path, 'utf8', function(err, str) {
if (err) return fn(err);
new(less.Parser)({
paths: [require('path').dirname(path)],
optimization: 0
}).parse(str, function(err, tree) {
if (err) return fn(err);
try {
css = tree.toCSS();
fn(null, css);
} catch (e) {
fn(e);
}
});
});
});
app.use('/css/', central.reqbase.css(__dirname + '/static'));
app.use(autostatic.middleware());
app.use(express.static(__dirname + '/static', conf.static_conf));
}
app.use(central.reqbase.errorHandler({ dump: conf.debug }));
app.use(central.reqbase.errorHandler.notFound);
// reference to the app running
central.app = app;
next && next(app);
}
// initial bootstraping
module.exports.boot = function() {
var app = central.servers['www'] = bootServer('www', conf.port);
var root_urls = {};
conf.servers.forEach(function(item) {
var hostname = item.hostname;
central.servers[hostname] = server = bootServer(hostname, item.port, app);
server.uri_root = item.root; // set the servers' uri root
root_urls[hostname.toUpperCase() + '_ROOT'] = item.root;
});
vhosts.forEach(function(host) {
var hostname = host[0];
var server = host[1];
server.log('info', 'vhosting..');
app.use(express.vhost(hostname + '.' + central.rootDomain, server));
});
// server boot callbacks
for (var hostname in central.servers) {
var server = central.servers[hostname];
// add helpers for server hosts
server.locals(root_urls);
server.ending && server.ending(server, app);
// for garbage collection..
delete server.ending;
}
};
module.exports.central = central;
if (!module.parent) {
//setTimeout(function() {
module.exports.boot();
//}, 20000);
}