From 26b8814567e01cce2d2ad56ffd4f2c0cb6de0301 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Tue, 19 Oct 2021 12:11:02 +0200 Subject: [PATCH] redirect service --- .github/workflows/docker.yaml | 40 ++++++++++++++++++++++++++++++++ .gitignore | 3 +++ Dockerfile | 10 ++++++++ config.yaml | 2 ++ go.mod | 5 ++++ go.sum | 4 ++++ main.go | 43 +++++++++++++++++++++++++++++++++++ 7 files changed, 107 insertions(+) create mode 100644 .github/workflows/docker.yaml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 config.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..805954b --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,40 @@ +name: Build docker + +on: + workflow_dispatch: + push: + tags: + - "v*.*.*" + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + install: true + - name: Docker Labels + id: meta + uses: crazy-max/ghaction-docker-meta@v2 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_REGISTRY_TOKEN }} + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ff7bbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Binaries +redirect +redirects diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b05f255 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:alpine as builder +WORKDIR /app +COPY go.* *.go config.yaml ./ +RUN go build +RUN ls -hal /app/redirects + +FROM alpine +COPY --from=builder /app/redirects /app/redirects +EXPOSE 80 +ENTRYPOINT [ "/app/redirects" ] diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..e5df8c9 --- /dev/null +++ b/config.yaml @@ -0,0 +1,2 @@ +- from: /0 + to: https://nms-ev.org diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f8ec25b --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/nms-ev/redirects + +go 1.17 + +require gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dd0bc19 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..2a84062 --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +import ( + _ "embed" + "log" + "net/http" + + "gopkg.in/yaml.v2" +) + +type ConfigEntry struct { + From string `yaml:"from"` + To string `yaml:"to"` +} + +type Config []ConfigEntry + +var ( + //go:embed config.yaml + data []byte + config Config +) + +func main() { + err := yaml.Unmarshal(data, &config) + if err != nil { + panic(err) + } + + for _, entry := range config { + tmp := entry + http.HandleFunc(tmp.From, func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, tmp.To, http.StatusTemporaryRedirect) + }) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 - Not Found")) + }) + + log.Fatal(http.ListenAndServe(":80", nil)) +}