From 24e305c8ff69332469c8fa65a8419b7c7665b04d Mon Sep 17 00:00:00 2001 From: nzambello Date: Thu, 7 Mar 2024 18:39:15 +0200 Subject: [PATCH] ci: add action --- .dockerignore | 3 ++ .gitea/workflows/publish.yml | 62 ++++++++++++++++++++++++++++++++++++ README.md | 1 + src/index.ts | 11 ++++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/publish.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9c71e3c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +node_modules +docs +.yarn diff --git a/.gitea/workflows/publish.yml b/.gitea/workflows/publish.yml new file mode 100644 index 0000000..6b92ff9 --- /dev/null +++ b/.gitea/workflows/publish.yml @@ -0,0 +1,62 @@ +name: Docker CI + +on: + push: + branches: + - main + +env: + node-version: 20.x + +jobs: + release: + runs-on: ubuntu-latest + container: + image: catthehacker/ubuntu:act-latest + env: + DOCKER_ORG: nzambello + DOCKER_LATEST: nightly + permissions: + contents: read + packages: write + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v4 + with: + images: | + nzambello/redirector + labels: | + org.label-schema.docker.cmd=docker run -d -p 8080:8080 nzambello/redirector + flavor: latest=false + tags: | + type=ref,event=branch + type=sha + type=raw,value=latest,enable={{is_default_branch}} + + - name: Login to Container Registry + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm64 + push: ${{ gitea.event_name != 'pull_request' }} + tags: | + nzambello/redirector:latest + labels: $${{ steps.meta.outputs.labels }} diff --git a/README.md b/README.md index 17fe2c0..9c2979d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ It loads configuration from environment variables: - `REDIRECT_URL`: (**required**) the url to redirect to - `PERMANENT`: if true, it will return a 301 status code, otherwise 302 +- `PRESERVE_PATH`: if true, it will preserve the path of the original request ### Docker example diff --git a/src/index.ts b/src/index.ts index 009f0b7..ed2a70c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,10 +10,19 @@ const app = new Hono(); app.get("*", etag(), logger()); app.get("/", (c) => { - const { REDIRECT_URL, PERMANENT } = env(c); + const { REDIRECT_URL, PERMANENT, PRESERVE_PATH } = env(c); invariant(REDIRECT_URL, "REDIRECT_URL environment variable is missing"); const isPermanentRedirect = PERMANENT && PERMANENT === "true"; + const shouldPreservePath = PRESERVE_PATH && PRESERVE_PATH === "true"; + + if (shouldPreservePath) { + const path = c.req.path; + return c.redirect( + `${REDIRECT_URL}${path}`, + isPermanentRedirect ? 301 : 302 + ); + } return c.redirect(REDIRECT_URL, isPermanentRedirect ? 301 : 302); });