feat: dev docker

This commit is contained in:
naiba 2024-10-24 21:33:36 +08:00
parent 31520debb2
commit 5cf8b8024c
4 changed files with 53 additions and 7 deletions

View File

@ -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

5
.gitignore vendored
View File

@ -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

View File

@ -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

View File

@ -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")
}