-
Notifications
You must be signed in to change notification settings - Fork 4
/
application.js
229 lines (190 loc) · 6.13 KB
/
application.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
'use strict';
const _ = require('./lib/lodash');
const http = require('http');
const compose = require('bay-compose');
const filter = require('filter-match').filter;
const Router = require('./lib/router/router');
const resolver = require('resolve-keypath').resolver;
const requireDir = require('require-dir');
const onFinished = require('on-finished');
const parse = require('parseurl');
const exceptions = require('./exceptions');
const statuses = require('statuses');
const path = require('path');
const PATH_SEPARATOR = '/'
class BayApplication {
constructor(options) {
if (!options) {
options = {};
}
this.env = process.env.NODE_ENV || 'development';
this.subdomainOffset = 2;
this.middleware = [];
this.getVersion = options.getVersion;
this.proxy = options.proxy;
this.base = options.base;
this.specifiedControllers = options.controllers
if (!this.base) {
throw new Error('missing base path');
}
this.router = new Router();
if (this.getVersion) {
this.getVersionTransformer = resolver(options.versions || requireDir(path.join(this.base, 'versions'), { recurse: true }), PATH_SEPARATOR);
}
}
getController (controllerName) {
if (!this._controllerResolver) {
this.initControllers()
this._controllerResolver = resolver(this.specifiedControllers, PATH_SEPARATOR)
}
return this._controllerResolver(controllerName);
}
initControllers () {
if (!this.specifiedControllers) {
// Lazy loading all controllers before processing any
// HTTP requests. That introduces an limitation that all
// routes defined after `#listen()` will be ignored.
this.specifiedControllers = this._requireControllers();
}
}
/**
* Load all controllers from disk
*
* @returns
* @private
* @memberof BayApplication
*/
_requireControllers () {
const ret = {}
this.router.getControllers().forEach((controllerName) => {
const pathParts = controllerName.split(PATH_SEPARATOR);
const subPath = path.join.apply(path, pathParts);
const requiredModule = require(path.join(this.base, 'controllers', subPath))
_.set(ret, pathParts, requiredModule)
})
return ret
}
/**
* Use the given middleware `fn`.
*
* @param {GeneratorFunction} fn
* @return {Application} self
* @api public
*/
use(fn) {
this.middleware.push(fn);
return this;
}
listen() {
this.initControllers();
const server = http.createServer(this.callback());
return server.listen.apply(server, arguments);
}
/**
* Return JSON representation.
* We only bother showing settings.
*
* @return {Object}
* @api public
*/
toJSON() {
return _.pick(this, ['subdomainOffset', 'proxy', 'env']);
}
callback() {
const self = this;
return function (req, res) {
res.statusCode = 404;
// Find the matching route
let match
try {
match = self.router.match(parse(req).pathname, req.method);
} catch (err) {
return self.handleError(req, res, err);
}
if (!match) {
return self.handleError(req, res, new exceptions.RoutingError('No route matches'));
}
// Resolve the controller
const actionName = match.handler.action;
const controllerName = match.handler.controller;
const ControllerClass = self.getController(controllerName);
if (!ControllerClass) {
return self.handleError(req, res, new exceptions.RoutingError(`Controller ${controllerName} not found`));
}
if (!ControllerClass.prototype[actionName]) {
return self.handleError(req, res,
new exceptions.RoutingError(`Action ${controllerName}#${actionName} not found`));
}
onFinished(res, function (err) {
if (err) {
self.handleError(req, res, convertToError(err));
}
});
const controller = new ControllerClass(self, req, res);
controller.route = match;
controller.params = match.params;
const middlewares = self.middleware.slice();
if (self.getVersion) {
const version = self.getVersion(controller);
const versionTransformer = self.getVersionTransformer(`${version}/${controllerName}`);
if (versionTransformer && versionTransformer[actionName]) {
middlewares.push(versionTransformer[actionName]);
}
}
if (controller._middleware) {
filter(actionName, controller._middleware).forEach(v => {
middlewares.push(typeof v.name === 'function' ? v.name : controller[v.name]);
});
}
middlewares.push(function *fillRespondBody(next) {
const fn = controller[actionName];
let body
if (typeof fn === 'function') {
const ret = fn.call(this);
// function, promise, generator, array, or object
if (ret != null && (typeof ret === 'object' || typeof ret === 'function')) {
body = yield ret;
} else {
body = ret;
}
}
if (typeof body !== 'undefined') {
controller.body = body;
}
yield next;
});
// Make Bay work with async function
compose(middlewares)(controller)
.then(function () { controller.respond() })
.catch(function (err) { self.handleError(req, res, convertToError(err)) });
};
}
handleError(req, res, err) {
if (res.headersSent || !res.socket || !res.socket.writable) {
err.headerSent = true;
return;
}
// unset all headers
res._headers = {};
// force text/plain
res.setHeader('Content-Type', 'text/plain');
// ENOENT support
if (err.code === 'ENOENT') {
err.status = 404;
}
// default to 500
if (typeof err.status !== 'number' || !statuses[err.status]) {
err.status = 500;
}
// respond
const msg = err.expose ? err.message : statuses[err.status];
res.statusCode = err.status;
res.setHeader('Content-Length', Buffer.byteLength(msg));
res.end(msg);
}
}
function convertToError(err) {
return err instanceof Error ? err : new Error(`non-error thrown: ${err}`);
}
module.exports = BayApplication;
exports.exceptions = exceptions;