diff --git a/src/server/index.ts b/src/server/index.ts index fa9960a..75cdc0c 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -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) diff --git a/src/server/request_handler/default_handler.ts b/src/server/request_handler/default_handler.ts new file mode 100644 index 0000000..57eb460 --- /dev/null +++ b/src/server/request_handler/default_handler.ts @@ -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 } + ); +} diff --git a/src/server/request_handler/page_assets_handler.ts b/src/server/request_handler/page_assets_handler.ts new file mode 100644 index 0000000..cad1348 --- /dev/null +++ b/src/server/request_handler/page_assets_handler.ts @@ -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 { + return serveFile( + request, + `./dist/route/${page}/${path}` + ) +} diff --git a/src/server/request_handler/page_handler.ts b/src/server/request_handler/page_handler.ts new file mode 100644 index 0000000..f326e2a --- /dev/null +++ b/src/server/request_handler/page_handler.ts @@ -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 { + return serveFile( + request, + `./dist/route/${page}/${defaultFile}` + ) +}