-
Notifications
You must be signed in to change notification settings - Fork 148
/
framework.js
executable file
·145 lines (126 loc) · 4.62 KB
/
framework.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
var http = require("http");
var url = require("url");
var cookie = require("./cookie");
var session = require("./session");
var config = require("./config");
var path = require("path");
var Context = require("./context").Context;
var Framework = function () {
this.sessionManager = new session.SessionManager(config.Timeout);
};
Framework.prototype.dispatch = function (request, response) {
if (request.url == "/favicon.ico") {
response.writeHead(404, "Not Found");
response.end();
return;
}
var routeInfo = this.route(request.url);
var controller;
try {
controller = require('./controllers/' + routeInfo.controller);
var method = request.method.toLowerCase() || 'get';
var action = controller[method] ? controller[method][routeInfo.action] : null;
if (action) {
this.enableGet(request, response);
this.enableCookie(request, response);
this.enablePost(request, response);
var curSession = this.enableSession(request, response);
// Pass request response session and framework into context object.
var context = new Context(request, response, curSession, this);
request.on("end", function () {
action.apply(context, routeInfo.args);
});
} else {
this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" without action "' + routeInfo.action + '" for "' + request.method + '" request.');
}
} catch (ex) {
console.log(ex.message);
console.log(ex.stack);
this.handler500(request, response, 'Error: Controller "' + routeInfo.controller + '" dosen\'t exsit.');
}
};
// Add get parse supports
Framework.prototype.enableGet = function () {
http.IncomingMessage.prototype.get = function (key) {
if (!this._urlMap) {
this._urlMap = url.parse(this.url, true);
}
return this._urlMap.query[key];
};
};
// Add cookie parse and set supports
Framework.prototype.enableCookie = function (request, response) {
http.IncomingMessage.prototype.cookie = function () {
this.cookie = function (key) {
if (!this._cookieMap) {
this._cookieMap = cookie.parse(this.headers.cookie || "");
}
return this._cookieMap[key];
};
};
http.ServerResponse.prototype.setCookie = function (cookieObj) {
if (!this._setCookieMap) {
this._setCookieMap = {};
}
this._setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
var returnVal = [];
for(var key in this._setCookieMap) {
returnVal.push(this._setCookieMap[key]);
}
this.setHeader("Set-Cookie", returnVal.join(", "));
};
};
// Add post parse supports
Framework.prototype.enablePost = function () {
http.IncomingMessage.prototype.post = function () {
if (!this._postMap) {
this._postMap = qs.parse(this.postData);
}
return this._postMap[key];
};
};
// Recept post body.
Framework.prototype.recept = function (request) {
if (request.method === "POST") {
var _postData = "";
this.on('data', function (chunk) {
_postData += chunk;
})
.on("end", function () {
request.postData = _postData;
});
}
};
// Add session supports
Framework.prototype.enableSession = function (request, response) {
var sessionManager = this.sessionManager;
var sessionId = request.cookie(session.SESSIONID_KEY);
var curSession;
if (sessionId && (curSession = sessionManager.get(sessionId))) {
if (sessionManager.isTimeout(curSession)) {
sessionManager.remove(sessionId);
curSession = sessionManager.renew(response);
} else {
curSession.updateTime();
}
} else {
curSession = sessionManager.renew(response);
}
return curSession;
};
Framework.prototype.handler500 = function (request, response, err) {
response.writeHead(500, {'Content-Type': 'text/plain'});
response.end(err);
};
Framework.prototype.route = function (requestUrl) {
// /controller/action/parameter1/parameter2
var pathname = url.parse(requestUrl).pathname;
var path = pathname.split("/");
path.shift(); // Remove the first "/"
return {
controller: path[0] || "index",
action: path[1] || "index",
args: path.slice(2) || [],
};
};
exports.Framework = Framework;