-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.ts
262 lines (241 loc) · 7.53 KB
/
application.ts
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
import { colors, HTTPSOptions, serve, serveTLS, Status } from "./deps.ts";
import { Router } from "./router.ts";
import { Middleware } from "./middleware.ts";
import { parseAddress } from "./utils/parse/address.ts";
import {
AppConfig,
HandlerFunc,
ListenOptions,
MethodFuncArgument,
MiddlewareFunc,
NextFunc,
Req,
ReqMethod,
Res,
RouteHandlers,
RoutesConfig,
RouteValue,
} from "./model.ts";
import { cors } from "./cors.ts";
import { assets } from "./assets.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
export const appConfig: AppConfig = {
server: {
port: 80,
hostname: "127.0.0.1",
},
};
export class Application {
#router: Router;
#middleware: Middleware;
request: Request;
response: Response;
constructor() {
this.#router = new Router();
this.#middleware = new Middleware();
this.request = new Request();
this.response = new Response();
}
#add = (method: ReqMethod, url: string, handlers: RouteHandlers) => {
const { handler, middleware } = handlers;
this.#router.add(method, url, handler, middleware || []);
};
use(func: MiddlewareFunc) {
if (typeof func !== "function") {
throw new Error("use function arguments must be a function type");
}
this.#middleware.push(func);
return this;
}
#setRoutes = async (routes: RoutesConfig[]) => {
for (let i = 0; i < routes.length; i++) {
const value = routes[i];
const method: ReqMethod = value.method.toLowerCase() as ReqMethod;
const { url, func, middleware = [] } = value;
const handler = func;
this.#add(method, url, { middleware, handler });
}
};
#parseHandler = (handlers: MethodFuncArgument): RouteHandlers => {
if (handlers.length < 1) {
throw new Error("router has no match url or handler function");
}
const handler = handlers.pop();
const res = {
handler,
} as RouteHandlers;
if (handlers.length) {
res.middleware = handlers;
}
return res;
};
get(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("get", path, funcHandler);
return this;
}
post(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("post", path, funcHandler);
return this;
}
put(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("put", path, funcHandler);
return this;
}
delete(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("delete", path, funcHandler);
return this;
}
options(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("options", path, funcHandler);
return this;
}
head(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("head", path, funcHandler);
return this;
}
connect(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("connect", path, funcHandler);
return this;
}
trace(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("trace", path, funcHandler);
return this;
}
patch(path: string, ...handlers: MethodFuncArgument) {
const funcHandler: RouteHandlers = this.#parseHandler(handlers);
this.#add("patch", path, funcHandler);
return this;
}
#composeMiddle = (
middleware: MethodFuncArgument,
request: Req,
response: Res,
execFunc: HandlerFunc | undefined,
) => {
if (!Array.isArray(middleware)) {
throw new TypeError("Middleware must be an array!");
}
return async function () {
let index = -1;
return dispatch(0);
async function dispatch(i: number): Promise<unknown> {
if (i <= index) {
return Promise.reject(new Error("next() called multiple times"));
}
index = i;
let fn: MiddlewareFunc | undefined = middleware[i];
if (i === middleware.length) {
fn = execFunc;
response.status = execFunc ? Status.OK : Status.NotFound;
}
try {
return fn &&
fn(request, response, dispatch.bind(null, index + 1) as NextFunc);
} catch (err) {
return Promise.reject(err);
}
}
};
};
#handleRequest = async (request: Req, response: Res) => {
const fv: RouteValue | null = this.#router.find(
request.method,
request.url,
);
const _m = this.#middleware.getMiddle();
const m: MethodFuncArgument = [];
let fn: HandlerFunc | undefined = undefined;
_m.forEach((value: MiddlewareFunc) => {
m.push(value);
});
if (fv) {
const { middleware, handler, params, query, url } = fv;
request.params = params;
request.url = url;
request.query = query;
fn = handler;
middleware.forEach((value: MiddlewareFunc) => {
m.push(value);
});
}
await this.request.parseBody(request);
// 注入参数只能ignore了
// deno-lint-ignore ban-ts-comment
// @ts-ignore
response.redirect = response.redirect.bind(globalThis, response);
// deno-lint-ignore ban-ts-comment
// @ts-ignore
response.render = response.render.bind(globalThis, response);
const f = this.#composeMiddle(m, request, response, fn);
await f.call(globalThis);
response.send(request, response);
};
// 后续补充配置项
// deno-lint-ignore no-explicit-any
#readConfig = async (config: any) => {
// config.constructor === undefined => config = await import('./min.config.ts')
config = config.constructor ? config : config.default;
const { server, routes, cors: corsConfig, assets: assetsConfig = "" } =
config;
// set server config
appConfig.server = server.addr
? { ...server, ...parseAddress(server.addr) }
: server;
routes?.length &&
await this.#setRoutes(routes);
corsConfig &&
this.use(cors(corsConfig));
this.use(assets(assetsConfig));
};
#listen = async (): Promise<void> => {
const server = appConfig.server;
const isTls = server.secure;
const protocol = isTls ? "https" : "http";
try {
const Server = isTls ? serveTLS(server as HTTPSOptions) : serve(server);
console.log(
colors.white(
`server is listening ${protocol}://${server.hostname}:${server.port} `,
),
);
for await (const request of Server) {
const req: Req = Request.createRequest({
url: request.url,
method: request.method.toLowerCase() as ReqMethod,
headers: request.headers,
request,
});
// 注入参数只能ignore了
// deno-lint-ignore ban-ts-comment
// @ts-ignore
const res: Res = Response.createResponse();
await this.#handleRequest(req, res);
}
} catch (e) {
throw new Error(e);
}
};
async listen(config: string | ListenOptions) {
if (typeof config === "string") {
appConfig.server = parseAddress(config);
} else {
appConfig.server = config as ListenOptions;
}
await this.#listen();
}
// 后续补充配置项
// deno-lint-ignore no-explicit-any
async start(config: any) {
await this.#readConfig(config);
await this.#listen();
}
}