Compare commits

..
4 Commits
Author SHA1 Message Date
naiba d62d0aa11c fix: home page 500 2024-11-29 22:49:17 +08:00
naiba b78c92b2df fix: swago 2024-11-29 21:55:29 +08:00
naiba 037a131b66 fix: ci 2024-11-29 21:42:18 +08:00
naiba 970c0d0430 feat: embed dist 2024-11-29 21:31:39 +08:00
11 changed files with 92 additions and 53 deletions
+27 -24
View File
@@ -26,8 +26,35 @@ jobs:
image: goreleaser/goreleaser-cross:v1.23
steps:
- run: git config --global --add safe.directory /__w/nezha/nezha
- uses: actions/checkout@v4
- uses: robinraju/release-downloader@v1
with:
repository: nezhahq/admin-frontend
tag: v1.0.4
fileName: dist.zip
latest: true
extract: true
- name: prepare admin-frontend dists
run: |
rm -rf cmd/dashboard/admin-dist
mv dist cmd/dashboard/admin-dist
- uses: robinraju/release-downloader@v1
with:
repository: nezhahq/user-frontend
tag: v1.0.3
fileName: dist.zip
latest: true
extract: true
- name: prepare admin-frontend dists
run: |
rm -rf cmd/dashboard/user-dist
mv dist cmd/dashboard/user-dist
- name: Fetch IPInfo GeoIP Database
env:
IPINFO_TOKEN: ${{ secrets.IPINFO_TOKEN }}
@@ -130,30 +157,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: robinraju/release-downloader@v1
with:
repository: nezhahq/admin-frontend
tag: v1.0.3
fileName: dist.zip
latest: true
extract: true
- name: prepare admin-frontend dists
run: |
mv dist admin-dist
- uses: robinraju/release-downloader@v1
with:
repository: nezhahq/user-frontend
tag: v1.0.2
fileName: dist.zip
latest: true
extract: true
- name: prepare admin-frontend dists
run: |
mv dist user-dist
- name: Download artifacts
uses: actions/download-artifact@v4
with:
+1 -1
View File
@@ -13,4 +13,4 @@ jobs:
with:
destination_repository: [email protected]:naibahq/nezha.git
destination_branch_name: master
destination_ssh_key: ${{ secrets.GITLAB_SSH_KEY }}
destination_ssh_key: ${{ secrets.GITEE_SSH_KEY }}
+2
View File
@@ -32,6 +32,8 @@ jobs:
- name: generate swagger docs
run: |
go install github.com/swaggo/swag/cmd/swag@latest
touch ./cmd/dashboard/user-dist/a
touch ./cmd/dashboard/admin-dist/a
swag init --pd -d . -g ./cmd/dashboard/main.go -o ./cmd/dashboard/docs --parseGoList=false
- name: Unit test
+4 -2
View File
@@ -17,8 +17,10 @@
.DS_Store
/cmd/dashboard/data
/cmd/dashboard/main
/cmd/dashboard/admin-dist
/cmd/dashboard/user-dist
/cmd/dashboard/admin-dist/*
/cmd/dashboard/user-dist/*
!/cmd/dashboard/admin-dist/.gitkeep
!/cmd/dashboard/user-dist/.gitkeep
/config.yml
/resource/template/theme-custom
/resource/static/custom
-2
View File
@@ -7,8 +7,6 @@ ARG TARGETOS
ARG TARGETARCH
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
COPY ./admin-dist /dashboard/admin-dist
COPY ./user-dist /dashboard/user-dist
COPY ./script/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
+1 -2
View File
@@ -40,9 +40,9 @@ You can change the dashboard language in the settings page (`/setting`) after th
## Contributors
<!--GAMFC_DELIMITER--><a href="https://github.com/naiba" title="naiba"><img src="https://avatars.githubusercontent.com/u/29243953?v=4" width="50;" alt="naiba"/></a>
<a href="https://github.com/uubulb" title="UUBulb"><img src="https://avatars.githubusercontent.com/u/35923940?v=4" width="50;" alt="UUBulb"/></a>
<a href="https://github.com/AkkiaS7" title="Akkia"><img src="https://avatars.githubusercontent.com/u/68485070?v=4" width="50;" alt="Akkia"/></a>
<a href="https://github.com/Erope" title="卖女孩的小火柴"><img src="https://avatars.githubusercontent.com/u/44471469?v=4" width="50;" alt="卖女孩的小火柴"/></a>
<a href="https://github.com/uubulb" title="UUBulb"><img src="https://avatars.githubusercontent.com/u/35923940?v=4" width="50;" alt="UUBulb"/></a>
<a href="https://github.com/nap0o" title="nap0o"><img src="https://avatars.githubusercontent.com/u/144927971?v=4" width="50;" alt="nap0o"/></a>
<a href="https://github.com/dysf888" title="黑歌"><img src="https://avatars.githubusercontent.com/u/47450409?v=4" width="50;" alt="黑歌"/></a>
<a href="https://github.com/xykt" title="xykt"><img src="https://avatars.githubusercontent.com/u/152045469?v=4" width="50;" alt="xykt"/></a>
@@ -86,7 +86,6 @@ You can change the dashboard language in the settings page (`/setting`) after th
<a href="https://github.com/Moraxyc" title="Moraxyc"><img src="https://avatars.githubusercontent.com/u/69713071?v=4" width="50;" alt="Moraxyc"/></a>
<a href="https://github.com/silver-ymz" title="Mingzhuo Yin"><img src="https://avatars.githubusercontent.com/u/78400701?v=4" width="50;" alt="Mingzhuo Yin"/></a>
<a href="https://github.com/MartijnLindeman" title="Martijn Lindeman"><img src="https://avatars.githubusercontent.com/u/78365708?v=4" width="50;" alt="Martijn Lindeman"/></a>
<a href="https://github.com/xrgzs" title="MadDogOwner"><img src="https://avatars.githubusercontent.com/u/26499123?v=4" width="50;" alt="MadDogOwner"/></a>
<a href="https://github.com/funnyzak" title="Leon"><img src="https://avatars.githubusercontent.com/u/2562087?v=4" width="50;" alt="Leon"/></a>
<a href="https://github.com/KorenKrita" title="KorenKrita"><img src="https://avatars.githubusercontent.com/u/22239339?v=4" width="50;" alt="KorenKrita"/></a>
<a href="https://github.com/techotaku" title="Ian Li"><img src="https://avatars.githubusercontent.com/u/1948179?v=4" width="50;" alt="Ian Li"/></a>
View File
+48 -21
View File
@@ -3,6 +3,8 @@ package controller
import (
"errors"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
@@ -21,7 +23,7 @@ import (
"github.com/nezhahq/nezha/service/singleton"
)
func ServeWeb() http.Handler {
func ServeWeb(adminFrontend, userFrontend fs.FS) http.Handler {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
@@ -37,12 +39,13 @@ func ServeWeb() http.Handler {
r.Use(waf.RealIp)
r.Use(waf.Waf)
r.Use(recordPath)
routers(r)
routers(r, adminFrontend, userFrontend)
return r
}
func routers(r *gin.Engine) {
func routers(r *gin.Engine, adminFrontend, userFrontend fs.FS) {
authMiddleware, err := jwt.New(initParams())
if err != nil {
log.Fatal("JWT Error:" + err.Error())
@@ -129,7 +132,7 @@ func routers(r *gin.Engine) {
auth.PATCH("/setting", commonHandler(updateConfig))
r.NoRoute(fallbackToFrontend)
r.NoRoute(fallbackToFrontend(adminFrontend, userFrontend))
}
func recordPath(c *gin.Context) {
@@ -208,25 +211,49 @@ 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
func fallbackToFrontend(adminFrontend, userFrontend fs.FS) func(*gin.Context) {
checkLocalFileOrFs := func(c *gin.Context, fs fs.FS, path string) bool {
if _, err := os.Stat(path); err == nil {
c.File(path)
return true
}
f, err := fs.Open(path)
if err != nil {
return false
}
defer f.Close()
fileStat, err := f.Stat()
if err != nil {
return false
}
if fileStat.IsDir() {
return false
}
http.ServeContent(c.Writer, c.Request, path, fileStat.ModTime(), f.(io.ReadSeeker))
return true
}
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 func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
return
}
c.File("admin-dist/index.html")
return
if strings.HasPrefix(c.Request.URL.Path, "/dashboard") {
stripPath := strings.TrimPrefix(c.Request.URL.Path, "/dashboard")
localFilePath := filepath.Join("admin-dist", stripPath)
if checkLocalFileOrFs(c, adminFrontend, localFilePath) {
return
}
if !checkLocalFileOrFs(c, adminFrontend, "admin-dist/index.html") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
}
return
}
localFilePath := filepath.Join("user-dist", c.Request.URL.Path)
if checkLocalFileOrFs(c, userFrontend, localFilePath) {
return
}
if !checkLocalFileOrFs(c, userFrontend, "user-dist/index.html") {
c.JSON(http.StatusOK, newErrorResponse(errors.New("404 Not Found")))
}
}
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")
}
+7 -1
View File
@@ -2,6 +2,8 @@ package main
import (
"context"
"embed"
_ "embed"
"flag"
"fmt"
"log"
@@ -32,6 +34,10 @@ type DashboardCliParam struct {
var (
dashboardCliParam DashboardCliParam
//go:embed admin-dist
adminFrontend embed.FS
//go:embed user-dist
userFrontend embed.FS
)
func initSystem() {
@@ -119,7 +125,7 @@ func main() {
singleton.NewServiceSentinel(serviceSentinelDispatchBus)
grpcHandler := rpc.ServeRPC()
httpHandler := controller.ServeWeb()
httpHandler := controller.ServeWeb(adminFrontend, userFrontend)
controller.InitUpgrader()
muxHandler := newHTTPandGRPCMux(httpHandler, grpcHandler)
View File
+2
View File
@@ -1,3 +1,5 @@
touch ./cmd/dashboard/user-dist/a
touch ./cmd/dashboard/admin-dist/a
swag init --pd -d . -g ./cmd/dashboard/main.go -o ./cmd/dashboard/docs --requiredByDefault
protoc --go-grpc_out="require_unimplemented_servers=false:." --go_out="." proto/*.proto
rm -rf ../agent/proto