-
Notifications
You must be signed in to change notification settings - Fork 29
/
routing.ts
45 lines (43 loc) · 1.19 KB
/
routing.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
import { IRouter } from "express";
import { CommonConfig } from "./config-type";
import { DependsOnMethod } from "./depends-on-method";
import { AbstractEndpoint } from "./endpoint";
import { AbstractLogger } from "./logger";
import { walkRouting } from "./routing-walker";
import { ServeStatic } from "./serve-static";
export interface Routing {
[SEGMENT: string]: Routing | DependsOnMethod | AbstractEndpoint | ServeStatic;
}
export const initRouting = ({
app,
rootLogger,
config,
routing,
}: {
app: IRouter;
rootLogger: AbstractLogger;
config: CommonConfig;
routing: Routing;
}) =>
walkRouting({
routing,
hasCors: !!config.cors,
onEndpoint: (endpoint, path, method, siblingMethods) => {
app[method](path, async (request, response) => {
const logger = config.childLoggerProvider
? await config.childLoggerProvider({ request, parent: rootLogger })
: rootLogger;
logger.info(`${request.method}: ${path}`);
await endpoint.execute({
request,
response,
logger,
config,
siblingMethods,
});
});
},
onStatic: (path, handler) => {
app.use(path, handler);
},
});