Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqxx committed Nov 5, 2024
1 parent 74eb92b commit 891f83c
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
import { route, type Route } from "@std/http/unstable-route";
import { STATUS_CODE } from "@std/http";
import { serveFile } from "@std/http";
import pageHandler from './request_handler/page_handler.ts';

const rootDirectory = './dist/';
const routeDirectory = `${rootDirectory}/route/`;
const appDirectory = `${rootDirectory}/app/`;

const routes: Route[] = [
{
pattern: new URLPattern({ pathname: "/-/:asset*" }),
pattern: new URLPattern({ pathname: "/-/:staticAsset*" }),
handler: (request, _info, parameters) => pageHandler(
request,
rootDirectory,
'static/',
parameters?.pathname.groups.asset
parameters?.pathname.groups.staticAsset
)
},
{
pattern: new URLPattern({ pathname: '/:page/' }),
handler: (request, _info, parameters) => pageHandler(
request,
routeDirectory,
parameters?.pathname.groups.page
)
pattern: new URLPattern({ pathname: '/:path*' }),
handler: (request, _info, parameters) => {
console.log(parameters?.pathname.groups.path)
return serveFile(request, appDirectory+'index.html')
}
},
{
pattern: new URLPattern({ pathname: '/:page/:asset*' }),
pattern: new URLPattern({ pathname: '/:page/:dynamicAsset*' }),
handler: (request, _info, parameters) => {
const {page, asset} = parameters?.pathname.groups ?? {};
const {page, dynamicAsset} = parameters?.pathname.groups ?? {};

return pageHandler(
request,
routeDirectory,
appDirectory,
page,
asset
dynamicAsset
);
}
},
{
pattern: new URLPattern({ pathname: '/' }),
handler: (request) => Response.redirect(request.url + 'home', STATUS_CODE.MovedPermanently)
}
];

Deno.serve(
{port: 8080},
route(routes, (request) => pageHandler(request, routeDirectory, '404'))
route(routes, (request) => pageHandler(request, appDirectory, '404'))
);

0 comments on commit 891f83c

Please sign in to comment.