-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http-middleware): basic http middleware
A basic http middleware to resolve route path and method to an action
- Loading branch information
1 parent
1e341b6
commit 9a12c86
Showing
3 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface Middleware { | ||
method: string; | ||
route: string; | ||
action: Function; | ||
} | ||
|
||
const middlewareStore: Array<Middleware> = []; | ||
|
||
export const createMiddleware = ( | ||
method: string, | ||
route: string, | ||
action: Function, | ||
) => { | ||
middlewareStore.push({ method, route, action }); | ||
}; | ||
|
||
export const getAllMiddlewares = (): Array<Middleware> => middlewareStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
Server, | ||
ServerRequest, | ||
} from "https://deno.land/std@0.53.0/http/server.ts"; | ||
import { getAllMiddlewares, Middleware } from "./middleware.ts"; | ||
|
||
interface Store { | ||
middlewares: null | Array<Middleware>; | ||
} | ||
|
||
const store: Store = { | ||
middlewares: null, | ||
}; | ||
|
||
const resolve = (request: ServerRequest) => { | ||
if (!store.middlewares) { | ||
store.middlewares = getAllMiddlewares(); | ||
} | ||
|
||
const middleware = store.middlewares.find((middleware: Middleware) => ( | ||
new RegExp(`^${middleware.route}$`).test(request.url) && | ||
( | ||
middleware.method == "*" || | ||
middleware.method == request.method | ||
) | ||
)); | ||
|
||
if (!middleware) { | ||
return request.respond({ status: 404, body: "Not found" }); | ||
} | ||
|
||
return middleware.action(request); | ||
}; | ||
|
||
const router = async (server: Server) => { | ||
for await (const request of server) { | ||
resolve(request); | ||
} | ||
}; | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
import server from "./http/server.ts"; | ||
import logger from "./logger/index.ts"; | ||
import router from "./http/router.ts"; | ||
import { createMiddleware } from "./http/middleware.ts"; | ||
import { ServerRequest } from "https://deno.land/std@0.53.0/http/server.ts"; | ||
|
||
for await (const request of server) { | ||
logger.info("Request received"); | ||
request.respond({ body: "Hello World" }); | ||
} | ||
createMiddleware("*", "/favicon.ico", (request: ServerRequest) => { | ||
return request.respond({ body: "" }); | ||
}); | ||
|
||
createMiddleware("GET", "/", (request: ServerRequest) => { | ||
return request.respond({ body: "GET - Hello world" }); | ||
}); | ||
|
||
createMiddleware("POST", "/", (request: ServerRequest) => { | ||
return request.respond({ body: "POST- Hello world" }); | ||
}); | ||
|
||
await router(server); |