chore: update hono, migrate to bun

This commit is contained in:
Nicola Zambello 2024-03-08 11:09:15 +02:00
parent bbd0da0473
commit a348329554
Signed by: nzambello
GPG key ID: 56E4A92C2C1E50BA
8 changed files with 25 additions and 1480 deletions

View file

@ -1 +0,0 @@
nodeLinker: node-modules

View file

@ -1,8 +1,6 @@
# syntax=docker/dockerfile:1
FROM node:18-alpine
FROM oven/bun:alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["yarn", "start"]
RUN bun install --production --frozen-lockfile
CMD ["bun", "start"]
EXPOSE 8787

View file

@ -1,7 +1,7 @@
# Redirector
Simple redirection service.
Runs on Node and uses [Hono](https://hono.dev).
Runs on [Bun](https://bun.sh) and uses [Hono](https://hono.dev).
Can be adapted to run on Bun, Cloudflare Workers, or any other serverless platform.
@ -12,7 +12,7 @@ Repository: [nzambello/redirector](https://git.nzambello.dev/nzambello/redirecto
```sh
yarn install
REDIRECT_URL=https://nzambello.dev PERMANENT=true PRESERVE_PATH=false yarn start
REDIRECT_URL=https://nzambello.dev PERMANENT=true PRESERVE_PATH=false bun start
```
Then, open http://localhost:8787/ in your browser.
@ -34,7 +34,7 @@ It loads configuration from environment variables:
### Docker example
```sh
docker run -p 8787:8787 -e REDIRECT_URL=https://nzambello.dev -e PERMANENT=true PRESERVE_PATH=false nzambello/redirector:latest
docker run -p 8787:8787 -e REDIRECT_URL=https://nzambello.dev -e PERMANENT=true -e PRESERVE_PATH=false nzambello/redirector:latest
```
### Docker compose example

BIN
bun.lockb Executable file

Binary file not shown.

View file

@ -1,4 +1,4 @@
version: '3.8'
version: "3.8"
services:
redirector:

View file

@ -3,13 +3,14 @@
"license": "Unlicense",
"main": "src/index.js",
"scripts": {
"start": "tsx src/index.ts"
"start": "bun src/index.ts",
"dev": "bun run --hot src/index.ts"
},
"dependencies": {
"@hono/node-server": "^1.0.1",
"@types/node": "^20.4.0",
"hono": "^3.2.7",
"@hono/node-server": "^1.8.2",
"@types/node": "^20.11.25",
"hono": "^4.0.10",
"tiny-invariant": "^1.3.1",
"tsx": "^3.12.7"
"tsx": "^4.7.1"
}
}

View file

@ -1,4 +1,3 @@
import { serve } from "@hono/node-server";
import { env } from "hono/adapter";
import { etag } from "hono/etag";
import { logger } from "hono/logger";
@ -7,10 +6,12 @@ import invariant from "tiny-invariant";
const app = new Hono();
app.get("*", etag(), logger());
app.get("/", (c) => {
const { REDIRECT_URL, PERMANENT, PRESERVE_PATH } = env(c);
app.get("*", etag(), logger(), (c) => {
const { REDIRECT_URL, PERMANENT, PRESERVE_PATH } = env<{
REDIRECT_URL: string;
PERMANENT: string | boolean | undefined;
PRESERVE_PATH: string | boolean | undefined;
}>(c);
invariant(REDIRECT_URL, "REDIRECT_URL environment variable is missing");
const isPermanentRedirect = PERMANENT && PERMANENT === "true";
@ -27,14 +28,9 @@ app.get("/", (c) => {
return c.redirect(REDIRECT_URL, isPermanentRedirect ? 301 : 302);
});
serve(
{
fetch: app.fetch,
port: 8787,
},
(info) => {
console.log(
`Server is running on (${info.family}) http://${info.address}:${info.port}`
);
}
);
console.log(`Server is running on http://0.0.0.0:8787`);
export default {
fetch: app.fetch,
port: 8787,
};

1449
yarn.lock

File diff suppressed because it is too large Load diff