Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqxx committed Oct 17, 2024
1 parent fdee6db commit 5ce4fc4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
import { route, type Route } from "@std/http/unstable-route";
import { serveFile } from "@std/http/file-server";
import defaultHandler from './request_handler/default_handler.ts';
import pageHandler from './request_handler/page_handler.ts';
import pageAssetsHandler from './request_handler/page_assets_handler.ts';

const routes: Route[] = [
{
pattern: new URLPattern({ pathname: "/static/:asset*" }),
handler: (request, _info, parameters) => serveFile(
request,
'./dist/static/' + parameters?.pathname.groups.asset
`./dist/static/${parameters?.pathname.groups.asset}`
)
},
{
pattern: new URLPattern({ pathname: '/:page([^\/]+\/?)' }),
handler: (request, _info, parameters) => serveFile(
request,
`./dist/route/${parameters?.pathname.groups.page}/index.html`
)
handler: (request, _info, parameters) => pageHandler(request, parameters?.pathname.groups.page)
},
{
pattern: new URLPattern({ pathname: '/:page/:asset*' }),
handler: (request, _info, parameters) => {
const groups = parameters?.pathname.groups;
return serveFile(
request,
`./dist/route/${groups?.page}/${groups?.asset}`
)
const {page, asset} = parameters?.pathname.groups ?? {}

return pageAssetsHandler(request, page, asset)
}
},
{
pattern: new URLPattern({ pathname: '/' }),
handler: (request) => serveFile(
request,
`./dist/route/home/index.html`
)
handler: (request) => pageHandler(request, 'home')
}
];

function defaultHandler(_req: Request) : Response {
return new Response("Not found", { status: 404 });
}

Deno.serve(
{port: 8080},
route(routes, defaultHandler)
Expand Down
11 changes: 11 additions & 0 deletions src/server/request_handler/default_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* This function handles default requests
*
* @returns The default response
*/
export default function defaultHandler() : Response {
return new Response(
"Not found",
{ status: 404 }
);
}
20 changes: 20 additions & 0 deletions src/server/request_handler/page_assets_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { serveFile } from '@std/http/file-server';

/**
* This function handles requests for page assets.
*
* @param request The request from the client
* @param page The requested page
* @param path The path to the asset that was requested
* @returns The asset response
*/
export default function pageAssetsHandler(
request : Request,
page : string | undefined,
path : string | undefined
) : Promise<Response> {
return serveFile(
request,
`./dist/route/${page}/${path}`
)
}
20 changes: 20 additions & 0 deletions src/server/request_handler/page_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { serveFile } from '@std/http/file-server';

/**
* This function handles page requests.
*
* @param request The request from the client
* @param page The requested page
* @param defaultFile The default file to serve
* @returns The page response
*/
export default function pageHandler(
request : Request,
page : string | undefined,
defaultFile = 'index.html'
) : Promise<Response> {
return serveFile(
request,
`./dist/route/${page}/${defaultFile}`
)
}

0 comments on commit 5ce4fc4

Please sign in to comment.