From abfbb698f2747bf9c6d01565e75154637074825c Mon Sep 17 00:00:00 2001 From: Snowykami Date: Mon, 16 Jun 2025 00:43:17 +0800 Subject: [PATCH] add GitHub Actions workflow and Dockerfile for building and pushing container image --- .github/workflows/build.yaml | 41 ++++++++++++++++++++++++++++++++++ .gitignore | 2 ++ Dockerfile | 33 +++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ main.go | 43 ++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 .github/workflows/build.yaml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..9111e23 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,41 @@ +name: Build and Push Container Image + +on: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: write + +concurrency: + group: docker-build + cancel-in-progress: false + +env: + CONTAINER_TAG: liteyuki/new-domain-redir:latest + REGISTRY: reg.liteyuki.org + +jobs: + build-and-push-and-deploy: + runs-on: liteyukios-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Log in to Liteyuki Harbor + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.LCR_USERNAME }} + password: ${{ secrets.LCR_PASSWORD }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build and push container image + uses: docker/build-push-action@v6 + with: + context: . + push: true + tags: ${{ env.REGISTRY }}/${{ env.CONTAINER_TAG }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a95508 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +.vscode/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4d6040a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +# build +FROM reg.liteyuki.org/dockerhub/golang:1.24.2-alpine3.21 AS builder + +ENV TZ=Asia/Chongqing + +WORKDIR /app + +RUN apk --no-cache add build-base git tzdata + +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . + +RUN go build -o main main.go + +# production +FROM reg.liteyuki.org/dockerhub/alpine:latest AS prod + +ENV TZ=Asia/Chongqing + +WORKDIR /app + +RUN apk --no-cache add tzdata ca-certificates libc6-compat libgcc libstdc++ + +COPY --from=builder /app/main /app/main + +EXPOSE 8888 + +RUN chmod +x ./main + +ENTRYPOINT ["./main"] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0622a19 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.liteyuki.org/LiteyukiStudio/new-domain-redir + +go 1.23.3 + +require github.com/cloudwego/hertz v0.10.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..803673e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/cloudwego/hertz v0.10.0 h1:V0vmBaLdQPlgL6w2TA6PZL1g6SGgQznFx6vqxWdCcKw= +github.com/cloudwego/hertz v0.10.0/go.mod h1:lRBohmcDkGx5TLK6QKFGdzJ6n3IXqGueHsOiXcYgXA4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..00b9b3f --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "github.com/cloudwego/hertz/pkg/app" + "github.com/cloudwego/hertz/pkg/app/server" + "strings" +) + +const ( + OldDomain = "liteyuki.icu" + NewDomain = "liteyuki.org" +) + +func main() { + h := server.Default() + h.Use(func(ctx context.Context, c *app.RequestContext) { + host := string(c.Host()) + if strings.HasSuffix(host, OldDomain) { + newHost := strings.TrimSuffix(host, OldDomain) + NewDomain + scheme := "https" + if proto := c.Request.Header.Get("X-Forwarded-Proto"); len(proto) > 0 { + scheme = string(proto) + } else if forwarded := c.Request.Header.Get("Forwarded"); len(forwarded) > 0 { + // Forwarded: proto=https;host=xxx + for _, part := range strings.Split(string(forwarded), ";") { + if strings.HasPrefix(part, "proto=") { + scheme = strings.TrimPrefix(part, "proto=") + break + } + } + } + newURL := scheme + "://" + newHost + string(c.Request.RequestURI()) + c.Redirect(301, []byte(newURL)) + c.Abort() + return + } + }) + h.GET("*", func(ctx context.Context, c *app.RequestContext) { + c.JSON(200, map[string]interface{}{"message": "pong"}) + }) + h.Spin() +}