summaryrefslogtreecommitdiff
path: root/cli/tools/init
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /cli/tools/init
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'cli/tools/init')
-rw-r--r--cli/tools/init/mod.rs41
1 files changed, 19 insertions, 22 deletions
diff --git a/cli/tools/init/mod.rs b/cli/tools/init/mod.rs
index 2d6a894e1..4e4a686c5 100644
--- a/cli/tools/init/mod.rs
+++ b/cli/tools/init/mod.rs
@@ -24,32 +24,29 @@ pub fn init_project(init_flags: InitFlags) -> Result<(), AnyError> {
create_file(
&dir,
"main.ts",
- r#"import { type Route, route, serveDir } from "@std/http";
+ r#"import { serveDir } from "@std/http";
-const routes: Route[] = [
- {
- pattern: new URLPattern({ pathname: "/" }),
- handler: () => new Response("Home page"),
- },
- {
- pattern: new URLPattern({ pathname: "/users/:id" }),
- handler: (_req, _info, params) => new Response(params?.pathname.groups.id),
- },
- {
- pattern: new URLPattern({ pathname: "/static/*" }),
- handler: (req) => serveDir(req),
- },
-];
-
-function defaultHandler(_req: Request) {
- return new Response("Not found", { status: 404 });
-}
-
-const handler = route(routes, defaultHandler);
+const userPagePattern = new URLPattern({ pathname: "/users/:id" });
+const staticPathPattern = new URLPattern({ pathname: "/static/*" });
export default {
fetch(req) {
- return handler(req);
+ const url = new URL(req.url);
+
+ if (url.pathname === "/") {
+ return new Response("Home page");
+ }
+
+ const userPageMatch = userPagePattern.exec(url);
+ if (userPageMatch) {
+ return new Response(userPageMatch.pathname.groups.id);
+ }
+
+ if (staticPathPattern.test(url)) {
+ return serveDir(req);
+ }
+
+ return new Response("Not found", { status: 404 });
},
} satisfies Deno.ServeDefaultExport;
"#,