-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttpd.js
368 lines (314 loc) · 11 KB
/
httpd.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var http = require('http');
var fs = require('fs');
var path = require('path');
var format = require("util").format;
var _extend = require("util")._extend;
var EventEmitter = require("events").EventEmitter;
var ActionVisitor = require("./libs/actionVisitor.js");
var Router = require("./libs/router.js");
var tplEngine = null;
var defineAction = Router.defineAction,
defineFilter = Router.defineFilter,
defineExtension = Router.defineExtension;
var isArray = Array.isArray;
var server = false , httpd = null , port , conf = {mapping:[], filter:[]} , appName = "";
var expressApp = null;
var expressMiddleware = false, reqProto = false, resProto = false;
var nope = function(){};
/**
* httpserver的处理,不在router中,而是在router之上,
* 直接在superAction中被拦截.用来减小router的复杂度.
*/
var services = [];
var rootRoter = null;
var dispatchServices = function(req,res,head){
//dispatch services
if(services.some(function(handle){
try{
return handle(req,res,head);
}catch(e){
log.err(e.stack);
res.statusCode = 500;
res.end(e.stack);
// 遇到crash不再继续;
return true;
}
})){
// process by service, return.
return true;
}
return false;
}
var superActions = function (req, res) {
var context, dispatch;
log.dev("%s %s " ,req.method, req.url);
/**
* 首先尝试services派发
*/
if(dispatchServices(req,res)){
return;
}
if(rootRoter){
context = new ActionVisitor(req,res,tplEngine);
if(expressMiddleware){
req.app = res.app = expressApp;
req.res = res;
res.req = req;
req.__proto__ = reqProto;
res.__proto__ = resProto;
req.next = nope;
}
// debugger;
dispatch = context.domain.bind(rootRoter.dispatch);
dispatch.call(rootRoter,context);
}else{
res.end("server not ready!");
}
};
var loadDefault = function(targetDir,type,doDefine){
var fullDir, reg_isJsFile = /(.+)\.js/i;
if(targetDir[0] != "/"){
fullDir = path.join(__dirname, targetDir);
}else{
fullDir = path.join(ROOT_DIR , targetDir);
}
/*
* 这里由于是启动过程中,所以直接使用同步处理
*/
if(fs.existsSync(fullDir)){
var afs = fs.readdirSync(fullDir);
afs.forEach(function(fname){
try{
var parts = reg_isJsFile.exec(fname) , fullPath , handle;
if(parts){
log.dev("httpd:load %s [%s]" ,type, fname);
fullPath = path.join(fullDir,fname);
handle = require(fullPath);
/**
* 如果载入的文件本身只是一个function,那么从文件名拆出名称.
* 如果载入的文件是一个具有name与handle的对像,那么直接使用
* 指定的名称和handle.
* 如果不满路以上两个条件,则进行忽略处理.
*/
if(handle instanceof Function){
doDefine(parts[1],false,handle);
}else if(handle.handle instanceof Function){
doDefine(handle.name || parts[1] , handle.depends, handle.handle);
}else{
log.warn("httpd:failed to load %s, because the [%s] is not a function or an object for define %s",type,fullPath,type);
}
}
}catch(e){
setTimeout(function(){
log.err("%s, at require file: %s",e.stack , fullPath);
},1);
}
});
}else{
log.warn("this directory is not exists, [%s]", fullDir);
}
}
httpd = _extend(new EventEmitter(),{
/**
* 启动httpserver
*/
start : function(_conf){
if(server){
log.warn("ignore call the httpd.start() after the server runing");
return;
}
if(_conf){
// 合并参数;
for(var key in _conf){
conf[key] = _conf[key];
}
/**
* 这将合并后的conf对像,重新进行定义rapid-httpserver.
* 并重新利用config的watch行为启动server
*/
conf.autoStart = true;
rapid.config.define("rapid-httpserver",conf);
return;
}
/**
* 直接将config中的内容处理为root router.
*/
if((!isArray(conf.mapping) || conf.mapping.length == 0) && conf.defaultAction == undefined){
log.warn("httpserver : no action , unable to work!!");
}
/**
*
* 默认载入行为移动至由definedplugin中移动到start中,是为让默认载入的内容,可以找到定义的conf
*
*/
//默认的extensions
loadDefault("./extension",'extension',function(a,b,c){
defineExtension(a, c || b);
});
// 默认action
loadDefault("./default_action","action",defineAction);
// 默认filter
loadDefault("./filter",'filter',defineFilter);
//自定义内容的载入位置
if(isArray(conf.loading_dir)){
conf.loading_dir.forEach(function(dir){
rapid.requireDir(dir);
});
}
/**
* 如果设置expressMiddleware,则启用express中间件的兼容.
* 可以将express的中间件做为filter使用.
*/
if(expressMiddleware = !!conf.expressMiddleware){
try{
var express = require("express");
expressApp = express();
reqProto = express.request;
resProto = express.response;
//ActionVisitor.prototype.__proto__ = expressApp;
}catch(e){
expressMiddleware = false;
log.warn("enable express middleware support, but can't require express");
}finally{
Router.prototype.expressMiddleware = expressMiddleware;
}
}
port = conf.port || 8080;
rootRoter = new Router({
filters:conf.filters || false,
mapping:conf.mapping || false,
/*
* 默认的错误处理名称为 error,
* 当未配置时,自动在定义的action列表中查找名称为error的action
*/
error : conf.errorAction || "error",
defaultAction:conf.defaultAction || function(){
var err = new Error("defaultAction not found!!");
this.sendStatus(500, err.message);
},
});
tplEngine = (function buildTplEngine(tplEngine){
try{
var enginePath;
if(!tplEngine){
return require("./libs/views");
}
switch(typeof(tplEngine)){
case "string" :
enginePath = path.join(ROOT_DIR,tplEngine)
log.info("httpd : load template engine from : %s" ,enginePath);
return require(enginePath);
case "object" :
if(tplEngine.render && tplEngine.renderStr){
return tplEngine;
}
default:
throw new Error("Invalid : config.tplConf.engine, use default : /libs/views.js");
}
}catch(e){
log.warn(e.stack);
return require("./libs/views");
}
})(conf.tplConfig && conf.tplConfig.engine);
tplEngine.conf && tplEngine.conf(conf.tplConfig);
server = http.createServer(superActions);
server.on("upgrade",dispatchServices);
server.listen(port , function(){
log.info("%s http server start runing, on port %d...", appName, port);
httpd.emit("start",server);
});
},
addService : function(handle){
var reg = /\n\s*/g;
var err = {} , stack;
Error.captureStackTrace(err, arguments.callee);
stack = err.stack;
stack = stack.split(reg);
stack.splice(0,1);
log.info("one service be add on, %s" , stack);
var index = services.indexOf(handle);
if(index == -1){
services.push(handle);
return true
}
return false;
},
removeService : function(handle){
var index = services.indexOf(handle);
if(index!= -1){
return services.splice(index,1);
}
return false;
},
/**
* 以下为以编程的方式扩展httpserver的一组接口,
* 每次使用同一个prefix时,将覆盖之前的配置.
*
* @param prefix {string} 将router应用到那一个前缀以下
* @param router {JSON} 一个具有指定格式的JSON对像.
*
*/
mount:function(prefix,router){
if (!(router instanceof Router)) {
router = new Router(conf);
}
return rootRoter.mount.apply(rootRouter,arguments);
},
createRouter:function(conf){
return new Route(conf);
},
/**
* 添加一个action.
*/
defineAction : function(name,depends,handle){
defineAction.apply(httpd,arguments);
},
/**
* 添加一个Extension
*/
defineExtension : function(name,handle){
defineExtension.apply(httpd,arguments);
},
/**
* 添加一个filter
*/
defineFilter : function(name,depends,handles){
defineFilter.apply(httpd,arguments);
},
// 几个短名称的快捷方式, 直接调用 defineXXXXXX.
action:function(){
defineAction.apply(this,arguments);
},
filter:function(){
defineFilter.apply(this,arguments);
},
extension:function(){
defineExtension.apply(this,arguments);
}
});
rapid.plugin.define("rapid-httpserver",['rapid-log'],function(log,cb){
if(server){
return;
}
appName = rapid.resource.appName || "clouda+";
log.info("Initialize rapid Http Server...");
cb && cb(null,httpd);
rapid.config.watch('rapid-httpserver',function(_conf){
if(server){
log.warn("ignore the configure change after the http server started.");
return;
}
if(conf){
log.info("overwrite the configure of rapid-httpserver before the server start;");
}
conf = _conf;
setImmediate(function(){
if(!conf.autoStart){
log.info("httpd.conf.autoStart is false, Waiting the httpd.start()");
}else{
log.info("httpd.conf.autoStart is true, self-acting the httpd.start()");
httpd.start();
}
});
},true);
});