Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS can import and run with one line #52

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<br />
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
39 changes: 0 additions & 39 deletions src/app.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app.test.ts → src/server.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
39 changes: 36 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -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);