-
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.
- Loading branch information
Showing
14 changed files
with
144 additions
and
79 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
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
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
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,28 +1,37 @@ | ||
import { z } from "zod"; | ||
import { BunicornApp } from "./index.ts"; | ||
import { BunicornApp, createMiddleware } from "./index.ts"; | ||
import { cacheMiddleware } from "./middleware/cacheMiddleware.ts"; | ||
import { Router } from "./router/base.ts"; | ||
|
||
const rb = new Router().output(z.object({ message: z.string() })); | ||
const loggerMiddleware = createMiddleware(async (_, next) => { | ||
console.log("Request received."); | ||
const response = await next(); | ||
console.log("Response sent with status:", response.status); | ||
return response; | ||
}); | ||
|
||
const countMiddleware = createMiddleware(async () => { | ||
console.log("Count middleware called."); | ||
return { count: 3 }; | ||
}); | ||
|
||
rb.input(z.object({ message: z.string() })).post("/hey", (ctx) => { | ||
return ctx.json({ message: "" }); | ||
const ageMiddleware = createMiddleware(async () => { | ||
console.log("Age middleware called."); | ||
return { age: 5 }; | ||
}); | ||
|
||
const getHelloMessage = new Router() | ||
.output(z.object({ msg: z.string() })) | ||
.get("/:id", async (ctx) => { | ||
return ctx.json({ | ||
msg: `Hello ${ctx.params.id}!`, | ||
}); | ||
const route = new Router() | ||
.use(loggerMiddleware) | ||
.use(cacheMiddleware({ by: (ctx) => ctx.req.url })) | ||
.use(countMiddleware) | ||
.use(ageMiddleware) | ||
.get("/", (ctx) => { | ||
console.log("Handler called."); | ||
return ctx.json({ message: `Hello World! ${ctx.count} ${ctx.age}` }); | ||
}); | ||
|
||
const app = new BunicornApp().addRoute(getHelloMessage).addRoute( | ||
new Router().get("/", (ctx) => { | ||
return ctx.json({ message: "Hello World!" }); | ||
}), | ||
); | ||
const app = new BunicornApp().addRoute(route); | ||
|
||
app.serve({ | ||
port: 8080, | ||
}); | ||
console.log("Server started on port 8080"); | ||
console.log("Server started on http://localhost:8080/"); |
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
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
import type { BunicornContext } from "../context/base.ts"; | ||
import type { BasePath } from "../router/types.ts"; | ||
|
||
export type BaseMiddleware<TPath extends BasePath = BasePath, TResult = any> = ( | ||
ctx: BunicornContext<TPath>, | ||
next: () => Promise<Response>, | ||
) => TResult | Promise<TResult>; | ||
|
||
export function createMiddleware<TResult>( | ||
cb: (ctx: BunicornContext, next: () => Promise<Response>) => TResult, | ||
) { | ||
return cb; | ||
} |
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,25 @@ | ||
import type { BunicornContext } from "../context/base.ts"; | ||
import { createMiddleware } from "./base.ts"; | ||
|
||
interface Args<MergeContext extends object> { | ||
by: (ctx: BunicornContext & MergeContext) => string | void; | ||
} | ||
|
||
export function cacheMiddleware<MergeContext extends object = {}>({ | ||
by, | ||
}: Args<MergeContext>) { | ||
const cache = new Map<unknown, Response>(); | ||
return createMiddleware(async (ctx, next) => { | ||
const key = by(ctx as BunicornContext & MergeContext); | ||
if (!key) { | ||
// No key given, just continue; | ||
return; | ||
} | ||
if (cache.has(key)) { | ||
return cache.get(key)!.clone(); | ||
} | ||
const response = await next(); | ||
cache.set(key, response.clone()); | ||
return response.clone(); | ||
}); | ||
} |
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 @@ | ||
export * from "./base.ts"; |
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
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
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
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
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