add custom not found handler
This commit is contained in:
		| @@ -3,6 +3,19 @@ import { HTTPServer } from "../http_server.ts"; | ||||
|  | ||||
| const httpServer = new HTTPServer(); | ||||
|  | ||||
| httpServer.error((req, _rep) => { | ||||
|   return JSON.stringify( | ||||
|     { | ||||
|       code: Status.NotFound, | ||||
|       message: "Route not found!", | ||||
|       path: req.path, | ||||
|       url: req.url | ||||
|     }, | ||||
|     null, | ||||
|     2, | ||||
|   ); | ||||
| }) | ||||
|  | ||||
| httpServer.add("GET", "/", (req, rep) => { | ||||
|   rep.status(Status.Teapot) | ||||
|     .header("working", "true") | ||||
|   | ||||
| @@ -28,6 +28,7 @@ export class HTTPServer { | ||||
|   private routes = new Map<string, Route>(); | ||||
|   private staticLocalDir?: string; | ||||
|   private staticServePath?: string; | ||||
|   private notFoundHandler?: RouteHandler; | ||||
|  | ||||
|   async listen(options: ListenOptions) { | ||||
|     this.server = Deno.listen({ | ||||
| @@ -56,6 +57,8 @@ export class HTTPServer { | ||||
|     for await (const requestEvent of httpConn) { | ||||
|       const url = new URL(requestEvent.request.url); | ||||
|       const filepath = decodeURIComponent(url.pathname); | ||||
|       const routeRequest = new RouteRequest(requestEvent.request); | ||||
|       const routeReply: RouteReply = new RouteReply(); | ||||
|  | ||||
|       if (this.staticServePath && filepath.startsWith(this.staticServePath)) { | ||||
|         const fileDir = filepath.split("/").slice(2).join("/"); | ||||
| @@ -69,6 +72,22 @@ export class HTTPServer { | ||||
|           file = await Deno.open(pathLoc, { read: true }); | ||||
|         } catch { | ||||
|           // If the file cannot be opened, return a "404 Not Found" response | ||||
|           if (this.notFoundHandler) { | ||||
|             routeReply.status(Status.NotFound); | ||||
|             routeReply.type("application/json"); | ||||
|             const notNoundHandle = await this.notFoundHandler( | ||||
|               routeRequest, | ||||
|               routeReply, | ||||
|             ); | ||||
|             await requestEvent.respondWith( | ||||
|               new Response(notNoundHandle as string, { | ||||
|                 status: routeReply.statusCode, | ||||
|                 headers: routeReply.headers, | ||||
|                 statusText: STATUS_TEXT[routeReply.statusCode], | ||||
|               }), | ||||
|             ); | ||||
|             continue; | ||||
|           } else { | ||||
|             await requestEvent.respondWith( | ||||
|               new Response( | ||||
|                 JSON.stringify({ | ||||
| @@ -77,9 +96,13 @@ export class HTTPServer { | ||||
|                 }), | ||||
|                 { | ||||
|                   status: Status.NotFound, | ||||
|                   headers: { | ||||
|                     "Content-Type": "application/json", | ||||
|                   }, | ||||
|                 }, | ||||
|               ), | ||||
|             ); | ||||
|           } | ||||
|           continue; | ||||
|         } | ||||
|  | ||||
| @@ -88,8 +111,6 @@ export class HTTPServer { | ||||
|         await requestEvent.respondWith(response); | ||||
|         return; | ||||
|       } | ||||
|       const routeRequest = new RouteRequest(requestEvent.request); | ||||
|       const routeReply: RouteReply = new RouteReply(); | ||||
|       const routeName = `${requestEvent.request.method}@${filepath}`; | ||||
|       let route = this.routes.has(routeName) | ||||
|         ? this.routes.get(routeName) | ||||
| @@ -141,7 +162,21 @@ export class HTTPServer { | ||||
|  | ||||
|         continue; | ||||
|       } | ||||
|  | ||||
|       if (this.notFoundHandler) { | ||||
|         routeReply.status(Status.NotFound); | ||||
|         routeReply.type("application/json"); | ||||
|         const notNoundHandle = await this.notFoundHandler( | ||||
|           routeRequest, | ||||
|           routeReply, | ||||
|         ); | ||||
|         await requestEvent.respondWith( | ||||
|           new Response(notNoundHandle as string, { | ||||
|             status: routeReply.statusCode, | ||||
|             headers: routeReply.headers, | ||||
|             statusText: STATUS_TEXT[routeReply.statusCode], | ||||
|           }), | ||||
|         ); | ||||
|       } else { | ||||
|         await requestEvent.respondWith( | ||||
|           new Response( | ||||
|             JSON.stringify({ | ||||
| @@ -150,11 +185,19 @@ export class HTTPServer { | ||||
|             }), | ||||
|             { | ||||
|               status: Status.NotFound, | ||||
|               headers: { | ||||
|                 "Content-Type": "application/json", | ||||
|               }, | ||||
|             }, | ||||
|           ), | ||||
|         ); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   error(handler: RouteHandler) { | ||||
|     this.notFoundHandler = handler; | ||||
|   } | ||||
|  | ||||
|   get(path: string, handler: RouteHandler) { | ||||
|     this.add("GET", path, handler); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user