diff --git a/README.md b/README.md index 8e6ad9f..e96d291 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ To do this, simply add the ref name to the sub-directory. 2. Run this command ```sh - deno run -A jsr:@5ouma/reproxy/serve + deno run -A jsr:@5ouma/reproxy ```
@@ -58,8 +58,7 @@ To do this, simply add the ref name to the sub-directory. 2. Replace the default code with this ```ts - import { app } from "jsr:@5ouma/reproxy"; - Deno.serve(app.fetch); + import "jsr:@5ouma/reproxy"; ``` 3. Set the environment variables diff --git a/deno.json b/deno.json index a928518..68ba1f5 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "name": "@5ouma/reproxy", "version": "1.0.0", - "exports": { ".": "./src/app.ts", "./serve": "./src/server.ts" }, + "exports": { ".": "./src/server.ts" }, "publish": { "include": ["LICENSE", "README.md", "deno.json", "src/"], "exclude": ["**/*.test.ts", "src/libs/test_utils.ts"] diff --git a/src/app.ts b/src/app.ts deleted file mode 100644 index a5f913b..0000000 --- a/src/app.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { type Context, Hono } from "@hono/hono"; -export type { Hono }; -import { logger } from "@hono/hono/logger"; -import { STATUS_CODE } from "@std/http/status"; -import { UserAgent } from "@std/http/user-agent"; - -import { checkRedirect, getContent } from "./libs/mod.ts"; - -/** - * The Hono application for this project. - * - * @example Access without a user agent - * ```ts - * const res: Response = await app.request("/"); - * ``` - * @example Access with a user agent - * ```ts - * const res: Response = await app.request("/", { - * headers: { "User-Agent": new UserAgent("Chrome/1.2.3").toString() }, - * }); - * ``` - */ -export const app: Hono = new Hono(); -app.use(logger()); -app - .get("/:ref?", async (ctx: Context) => { - const ref: string = ctx.req.param("ref"); - const url: URL | null = checkRedirect( - new UserAgent(ctx.req.header("User-Agent") ?? ""), - ref, - ); - - return url - ? ctx.redirect(url.toString(), STATUS_CODE.PermanentRedirect) - : ctx.text(...await getContent(ref)); - }) - .get("*", (ctx: Context) => { - return ctx.redirect("/", STATUS_CODE.SeeOther); - }); diff --git a/src/app.test.ts b/src/server.test.ts similarity index 97% rename from src/app.test.ts rename to src/server.test.ts index 2fd146c..9340a1e 100644 --- a/src/app.test.ts +++ b/src/server.test.ts @@ -1,7 +1,7 @@ import { assertEquals } from "@std/assert"; import { STATUS_CODE } from "@std/http/status"; -import { app } from "./app.ts"; +import { app } from "./server.ts"; import { exportRepo, testRef, diff --git a/src/server.ts b/src/server.ts index add4720..2b16058 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,8 +1,41 @@ +import { type Context, Hono } from "@hono/hono"; +export type { Hono }; +import { logger } from "@hono/hono/logger"; +import { STATUS_CODE } from "@std/http/status"; +import { UserAgent } from "@std/http/user-agent"; + +import { checkRedirect, getContent } from "./libs/mod.ts"; + /** - * This file is the entry point for the server. - * @module + * The Hono application for this project. + * + * @example Access without a user agent + * ```ts + * const res: Response = await app.request("/"); + * ``` + * @example Access with a user agent + * ```ts + * const res: Response = await app.request("/", { + * headers: { "User-Agent": new UserAgent("Chrome/1.2.3").toString() }, + * }); + * ``` */ +export const app: Hono = new Hono(); +app.use(logger()); +app + .get("/:ref?", async (ctx: Context) => { + const ref: string = ctx.req.param("ref"); + const url: URL | null = checkRedirect( + new UserAgent(ctx.req.header("User-Agent") ?? ""), + ref, + ); -import { app } from "./app.ts"; + return url + ? ctx.redirect(url.toString(), STATUS_CODE.PermanentRedirect) + : ctx.text(...await getContent(ref)); + }) + .get("*", (ctx: Context) => { + return ctx.redirect("/", STATUS_CODE.SeeOther); + }); Deno.serve(app.fetch);