add preprocessor handlers
This commit is contained in:
parent
0675b26dad
commit
03a1f35eaa
|
@ -4,8 +4,11 @@ import { HTTPServer } from "../mod.ts";
|
||||||
|
|
||||||
const httpServer = new HTTPServer();
|
const httpServer = new HTTPServer();
|
||||||
|
|
||||||
httpServer.middleware(async (req, rep, done) => {
|
httpServer.preprocessor((_req, rep) => {
|
||||||
rep.header("Access-Control-Allow-Origin", "*");
|
rep.header("Access-Control-Allow-Origin", "*");
|
||||||
|
});
|
||||||
|
|
||||||
|
httpServer.middleware(async (req, _rep, done) => {
|
||||||
console.log(`${req.method} - ${req.remoteIpAddr} - ${req.path}`);
|
console.log(`${req.method} - ${req.remoteIpAddr} - ${req.path}`);
|
||||||
const processTime = await done();
|
const processTime = await done();
|
||||||
console.log(`Processed in ${prettyTime(processTime)}`);
|
console.log(`Processed in ${prettyTime(processTime)}`);
|
||||||
|
|
19
mod.ts
19
mod.ts
|
@ -25,6 +25,11 @@ type RouteMiddlewareHandler = (
|
||||||
done: () => Promise<number[]>,
|
done: () => Promise<number[]>,
|
||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
|
|
||||||
|
type RoutePreprocessor = (
|
||||||
|
req: RouteRequest,
|
||||||
|
rep: RouteReply,
|
||||||
|
) => void;
|
||||||
|
|
||||||
type RouteParam = {
|
type RouteParam = {
|
||||||
idx: number;
|
idx: number;
|
||||||
paramKey: string;
|
paramKey: string;
|
||||||
|
@ -36,6 +41,7 @@ export class HTTPServer {
|
||||||
private staticLocalDir?: string;
|
private staticLocalDir?: string;
|
||||||
private staticServePath?: string;
|
private staticServePath?: string;
|
||||||
private notFoundHandler?: RouteHandler;
|
private notFoundHandler?: RouteHandler;
|
||||||
|
private preprocessors: RoutePreprocessor[] = [];
|
||||||
private middlewareHandler?: RouteMiddlewareHandler;
|
private middlewareHandler?: RouteMiddlewareHandler;
|
||||||
|
|
||||||
async listen(options: ListenOptions) {
|
async listen(options: ListenOptions) {
|
||||||
|
@ -116,6 +122,10 @@ export class HTTPServer {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.preprocessors.forEach((preProcessor) =>
|
||||||
|
preProcessor(routeRequest, routeReply)
|
||||||
|
);
|
||||||
|
|
||||||
let resolveAction: (value: number[]) => void = () => {};
|
let resolveAction: (value: number[]) => void = () => {};
|
||||||
let middlewarePromise;
|
let middlewarePromise;
|
||||||
const perStart = performance.now();
|
const perStart = performance.now();
|
||||||
|
@ -198,7 +208,10 @@ export class HTTPServer {
|
||||||
(accum: { [key: string]: string }, curr: RouteParam) => {
|
(accum: { [key: string]: string }, curr: RouteParam) => {
|
||||||
return {
|
return {
|
||||||
...accum,
|
...accum,
|
||||||
[curr.paramKey]: routeSegments[curr.idx].replace(/(?!\/)\W\D.*/gm, ""),
|
[curr.paramKey]: routeSegments[curr.idx].replace(
|
||||||
|
/(?!\/)\W\D.*/gm,
|
||||||
|
"",
|
||||||
|
),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
{},
|
{},
|
||||||
|
@ -237,6 +250,10 @@ export class HTTPServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preprocessor(handler: RoutePreprocessor) {
|
||||||
|
this.preprocessors.push(handler);
|
||||||
|
}
|
||||||
|
|
||||||
middleware(handler: RouteMiddlewareHandler) {
|
middleware(handler: RouteMiddlewareHandler) {
|
||||||
this.middlewareHandler = handler;
|
this.middlewareHandler = handler;
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user