Skip to content

Commit

Permalink
replace rt with rtx
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Apr 6, 2024
1 parent f282618 commit f8f9ba6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
75 changes: 45 additions & 30 deletions cli/main.ts → cli/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRouter } from "@fartlabs/rt";
import { Delete } from "@fartlabs/rtx";
import { Get, Post, Router } from "@fartlabs/rtx";
import { go } from "go/go.ts";

type Shortlinks = Record<string, string>;
Expand Down Expand Up @@ -61,37 +62,51 @@ function isAuthorized(headers: Headers): boolean {
if (import.meta.main) {
const kv = await Deno.openKv();
const goService = new GoService(kv);
const router = createRouter()
// TODO: Use rtx to define the routes. Use htx to define the HTML index page.
.post("/api", async (ctx) => {
if (!isAuthorized(ctx.request.headers)) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
const router = (
<Router>
<Post
pattern="/api"
handle={async (ctx) => {
if (!isAuthorized(ctx.request.headers)) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

const body = await ctx.request.json();
await goService.add(body.alias, body.destination, body.force);
return Response.json({ message: "Shortlink created." }, { status: 201 });
})
.delete("/api", async (ctx) => {
if (!isAuthorized(ctx.request.headers)) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
const body = await ctx.request.json();
await goService.add(body.alias, body.destination, body.force);
return Response.json(
{ message: "Shortlink created." },
{ status: 201 },
);
}}
/>
<Delete
pattern="/api"
handle={async (ctx) => {
if (!isAuthorized(ctx.request.headers)) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

const body = await ctx.request.json();
await goService.delete(body.alias);
return Response.json({ message: "Shortlink deleted." });
})
.get("/:path*", async (ctx) => {
const shortlinks = await goService.shortlinks();
const destination = go(ctx.url, shortlinks);
return new Response(
`Going to ${destination.href}...`,
{
status: 302,
headers: { "Location": destination.href },
},
);
});
const body = await ctx.request.json();
await goService.delete(body.alias);
return Response.json({ message: "Shortlink deleted." });
}}
/>
<Get
pattern="/:path*"
handle={async (ctx) => {
const shortlinks = await goService.shortlinks();
const destination = go(ctx.url, shortlinks);
return new Response(
`Going to ${destination.href}...`,
{
status: 302,
headers: { "Location": destination.href },
},
);
}}
/>
</Router>
);

Deno.serve((request) => router.fetch(request));
}
13 changes: 9 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
{
"lock": false,
"imports": {
"go/": "./",
"@fartlabs/rt": "jsr:@fartlabs/rt@^0.0.1",
"@std/assert": "jsr:@std/assert@^0.221.0"
"@fartlabs/jsonx": "jsr:@fartlabs/jsonx@^0.0.10",
"@fartlabs/rtx": "jsr:@fartlabs/rtx@^0.0.2",
"@std/assert": "jsr:@std/assert@^0.221.0",
"go/": "./"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "@fartlabs/jsonx"
},
"tasks": {
"start": "deno run --unstable-kv --env -A cli/main.ts"
"start": "deno run --unstable-kv --env -A cli/main.tsx"
}
}

0 comments on commit f8f9ba6

Please sign in to comment.