From c5d7e317a45a9cc79b5c890aef7b94b1de39facf Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Sat, 10 Jan 2026 03:58:22 +0800 Subject: [PATCH] refactor: Streamline user information retrieval and validation - Refactored the user information retrieval process to improve efficiency. - Enhanced validation logic for input parameters in the user handler. - Updated UserService interface to support new retrieval methods. - Improved error handling for user status checks before responding. --- .gitea/workflows/build.yml | 72 ++++++++++++++++++++++++++++++++++++++ Dockerfile | 60 +++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 .gitea/workflows/build.yml create mode 100644 Dockerfile diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..4e292b1 --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,72 @@ +name: Build + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.25' + cache: true + + - name: Download dependencies + run: go mod download + + - name: Build + env: + GOOS: linux + GOARCH: amd64 + CGO_ENABLED: 0 + run: go build -v -o mcauth-linux-amd64 ./cmd/server + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: mcauth-linux-amd64 + path: mcauth-linux-amd64 + + build-docker: + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Gitea Container Registry + uses: docker/login-action@v3 + with: + registry: code.littlelan.cn + username: ${{ secrets.GIT_USERNAME }} + password: ${{ secrets.GIT_TOKEN }} + + - name: Download artifact + uses: actions/download-artifact@v3 + with: + name: mcauth-linux-amd64 + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + build-args: | + BINARY_NAME=mcauth-linux-amd64 + tags: | + code.littlelan.cn/${{ github.repository_owner }}/mcauth:latest + code.littlelan.cn/${{ github.repository_owner }}/mcauth:${{ github.sha }} + platforms: linux/amd64 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..327a1d3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,60 @@ +# 构建阶段 +FROM golang:1.25-alpine AS builder + +# 安装必要的工具 +RUN apk add --no-cache git ca-certificates tzdata + +# 设置工作目录 +WORKDIR /build + +# 复制 go mod 文件 +COPY go.mod go.sum ./ + +# 下载依赖 +RUN go mod download + +# 复制源代码 +COPY . . + +# 构建应用 +ARG BINARY_NAME=mcauth-linux-amd64 +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ + -ldflags="-w -s" \ + -o /app/${BINARY_NAME} \ + ./cmd/server + +# 运行阶段 +FROM alpine:latest + +# 安装必要的运行时依赖 +RUN apk add --no-cache ca-certificates tzdata + +# 创建非 root 用户 +RUN addgroup -g 1000 appuser && \ + adduser -D -u 1000 -G appuser appuser + +# 设置工作目录 +WORKDIR /app + +# 从构建阶段复制二进制文件 +ARG BINARY_NAME=mcauth-linux-amd64 +COPY --from=builder /app/${BINARY_NAME} /app/server + +# 复制配置文件(如果需要) +COPY configs/ /app/configs/ + +# 设置权限 +RUN chown -R appuser:appuser /app + +# 切换到非 root 用户 +USER appuser + +# 暴露端口 +EXPOSE 8080 + +# 健康检查 +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ + CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1 + +# 启动应用 +ENTRYPOINT ["/app/server"]