From 5cf8b8024cb16fa35717be28531142306c0c1aa7 Mon Sep 17 00:00:00 2001 From: naiba Date: Thu, 24 Oct 2024 21:33:36 +0800 Subject: [PATCH] feat: dev docker --- .github/workflows/release.yml | 24 ++++++++++++++++++--- .gitignore | 5 +++-- Dockerfile | 1 + cmd/dashboard/controller/controller.go | 30 ++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 986d05a..f68029c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,6 +4,8 @@ on: push: tags: - "v*" + branches: + - dev jobs: build: @@ -33,7 +35,7 @@ jobs: - uses: actions/checkout@v4 - name: Fetch IPInfo GeoIP Database - if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + if: github.event_name == 'push' env: IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }} run: | @@ -81,6 +83,7 @@ jobs: done - name: Release + if: contains(github.ref, 'refs/tags/') uses: ncipollo/release-action@v1 with: artifacts: "assets/*/*/*.zip" @@ -99,6 +102,7 @@ jobs: curl -s https://purge.jsdelivr.net/gh/$LOWER_USERNAME/nezha@master/script/config.yaml - name: Trigger sync + if: contains(github.ref, 'refs/tags/') env: GH_REPO: ${{ github.repository }} GH_TOKEN: ${{ github.token }} @@ -108,7 +112,7 @@ jobs: release-docker: runs-on: ubuntu-latest - if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') + if: github.event_name == 'push' needs: build name: Release Docker images steps: @@ -124,12 +128,26 @@ jobs: chmod -R +x ./assets/* mkdir dist mv ./assets/*/*/* ./dist + + - name: prepare frontend dists + run: | + wget https://github.com/nezhahq/nezha-dashboard/releases/download/v0.0.2/dist.zip + unzip dist.zip + mv dist admin-dist - - name: Extract branch name + - name: Extract branch name in tag + if: contains(github.ref, 'refs/tags/') run: | export TAG_NAME=$(echo ${GITHUB_REF#refs/tags/}) echo "tag=$TAG_NAME" >> $GITHUB_OUTPUT id: extract_branch + + - name: Extract branch name in branch + if: not(contains(github.ref, 'refs/tags/')) + run: | + export TAG_NAME=$(echo ${GITHUB_REF#refs/heads/}) + echo "tag=$TAG_NAME" >> $GITHUB_OUTPUT + id: extract_branch - name: Log into GHCR uses: docker/login-action@master diff --git a/.gitignore b/.gitignore index ba67c4e..89b21d4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,11 +12,12 @@ *.out *.pprof .idea -/data /dist .DS_Store -/main +/cmd/dashboard/data /cmd/dashboard/main +/cmd/dashboard/admin-dist +/cmd/dashboard/user-dist /config.yml /resource/template/theme-custom /resource/static/custom diff --git a/Dockerfile b/Dockerfile index 636fecc..a5503eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ ARG TARGETOS ARG TARGETARCH COPY --from=certs /etc/ssl/certs /etc/ssl/certs +COPY ./frontend /dashboard/frontend COPY ./script/entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh diff --git a/cmd/dashboard/controller/controller.go b/cmd/dashboard/controller/controller.go index 0a3df45..21fd7a3 100644 --- a/cmd/dashboard/controller/controller.go +++ b/cmd/dashboard/controller/controller.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "net/http" + "os" + "path/filepath" "strings" jwt "github.com/appleboy/gin-jwt/v2" @@ -13,7 +15,6 @@ import ( swaggerfiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" - docs "github.com/naiba/nezha/cmd/dashboard/docs" "github.com/naiba/nezha/model" "github.com/naiba/nezha/service/singleton" ) @@ -21,7 +22,7 @@ import ( func ServeWeb() http.Handler { gin.SetMode(gin.ReleaseMode) r := gin.Default() - docs.SwaggerInfo.BasePath = "/api/v1" + if singleton.Conf.Debug { gin.SetMode(gin.DebugMode) pprof.Register(r) @@ -90,6 +91,8 @@ func routers(r *gin.Engine) { auth.POST("/ddns", commonHandler(createDDNS)) auth.PATCH("/ddns/:id", commonHandler(updateDDNS)) auth.POST("/batch-delete/ddns", commonHandler(batchDeleteDDNS)) + + r.NoRoute(fallbackToFrontend) } func recordPath(c *gin.Context) { @@ -144,3 +147,26 @@ func commonHandler[T any](handler handlerFunc[T]) func(*gin.Context) { } } } + +func fallbackToFrontend(c *gin.Context) { + if strings.HasPrefix(c.Request.URL.Path, "/api") { + c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found"))) + return + } + if strings.HasPrefix(c.Request.URL.Path, "/dashboard") { + stripPath := strings.TrimPrefix(c.Request.URL.Path, "/dashboard") + localFilePath := filepath.Join("./admin-dist", stripPath) + if _, err := os.Stat(localFilePath); err == nil { + c.File(localFilePath) + return + } + c.File("admin-dist/index.html") + return + } + localFilePath := filepath.Join("user-dist", c.Request.URL.Path) + if _, err := os.Stat(localFilePath); err == nil { + c.File(localFilePath) + return + } + c.File("user-dist/index.html") +}