chore: improve bundling + refactor router (#10)
This commit changes how bundling works, by bundling all routes at once. Static JS assets are now cached immutably. Additionally this PR completly refactors how the router works to make it more modular. This allows users to opt out of the default oak server, and instead create their own.
This commit is contained in:
+26
-6
@@ -1,13 +1,33 @@
|
||||
import { createPages, PageModules } from "./routes.ts";
|
||||
import { createServer } from "./server.ts";
|
||||
import { oak } from "./deps.ts";
|
||||
import { Page, PageModule, processRoutes } from "./page.ts";
|
||||
import { installRoutes } from "./router.ts";
|
||||
|
||||
export function setup(pageModules: [PageModules, string][], selfUrl: string) {
|
||||
const baseUrl = new URL("./", selfUrl).href;
|
||||
const pages = createPages(pageModules, baseUrl);
|
||||
const app = createServer(pages);
|
||||
export interface Routes {
|
||||
pages: Record<string, PageModule>;
|
||||
baseUrl: string;
|
||||
}
|
||||
|
||||
export { installRoutes };
|
||||
|
||||
export function start(routes: Routes) {
|
||||
const pages = processRoutes(routes);
|
||||
const app = createDefaultServer(pages);
|
||||
app.addEventListener("error", (err) => {
|
||||
console.error(err.error);
|
||||
});
|
||||
|
||||
addEventListener("fetch", app.fetchEventHandler());
|
||||
}
|
||||
|
||||
export function createDefaultServer(pages: Page[]): oak.Application {
|
||||
const router = new oak.Router();
|
||||
|
||||
installRoutes(router, pages);
|
||||
|
||||
const app = new oak.Application();
|
||||
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user