From 889b0bce054b9968279bfdb50eec65d1e719244e Mon Sep 17 00:00:00 2001 From: zhanghan Date: Fri, 1 Sep 2023 09:57:53 +0800 Subject: [PATCH] add static handler bind --- server/main.go | 1 + server/router/router.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/server/main.go b/server/main.go index 7202a05..7d5db8f 100644 --- a/server/main.go +++ b/server/main.go @@ -38,6 +38,7 @@ func main() { global.GlobalClient = client.DefaultClient(plugin.Init(config.Config().Prometheus)) router.RegisterAPIs(server) + router.StaticRouter(server) global.GlobalClient.Server = config.Config().Http.Addr if err := server.Run(config.Config().Http.Addr); err != nil { diff --git a/server/router/router.go b/server/router/router.go index 1067c2b..3b16a1b 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -1,6 +1,9 @@ package router import ( + "net/http" + "strings" + "gitee.com/openeuler/PilotGo-plugins/sdk/logger" "github.com/gin-gonic/gin" "openeuler.org/PilotGo/prometheus-plugin/global" @@ -55,3 +58,18 @@ func RegisterAPIs(router *gin.Engine) { targetManager.DELETE("delTarget", httphandler.DeletePrometheusTarget) } } + +func StaticRouter(router *gin.Engine) { + router.Static("/static", "../web/dist/static") + router.StaticFile("/", "../web/dist/index.html") + + // 解决页面刷新404的问题 + router.NoRoute(func(c *gin.Context) { + logger.Info("process noroute: %s", c.Request.URL.RawPath) + if !strings.HasPrefix(c.Request.RequestURI, "/api/") && !strings.HasPrefix(c.Request.RequestURI, "/plugin/Prometheus") { + c.File("./web/dist/index.html") + return + } + c.AbortWithStatus(http.StatusNotFound) + }) +} -- Gitee