From aa2927e55dfa0051c81c355fa998f54eff61e686 Mon Sep 17 00:00:00 2001 From: ehco1996 Date: Mon, 17 Jan 2022 09:45:12 +0800 Subject: [PATCH 1/3] savework --- dm/ui/server.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/dm/ui/server.go b/dm/ui/server.go index baeb5fd8c1f..7c7911e221d 100644 --- a/dm/ui/server.go +++ b/dm/ui/server.go @@ -18,6 +18,7 @@ package ui import ( "io/fs" "net/http" + "strings" "github.com/gin-gonic/gin" "github.com/pingcap/tiflow/dm/openapi" @@ -25,20 +26,40 @@ import ( "go.uber.org/zap" ) +const ( + buildPath = "dist" + assetsPath = "assets" + indexPath = "/dashboard/" +) + // WebUIAssetsHandler returns a http handler for serving static files. func WebUIAssetsHandler() http.FileSystem { - stripped, err := fs.Sub(WebUIAssets, "dist") + stripped, err := fs.Sub(WebUIAssets, buildPath) if err != nil { panic(err) // this should never happen } return http.FS(stripped) } +// we need this to handel this case: user want to access /dashboard/source.html/ but webui is a single page app. +// and it only can handel requests in index page, so we need to redirect to index page +func alwaysRedirect(path string) gin.HandlerFunc { + return func(c *gin.Context) { + if c.Request.URL.Path != path && !strings.Contains(c.Request.URL.Path, assetsPath) { + c.Redirect(http.StatusPermanentRedirect, path) + c.AbortWithStatus(http.StatusPermanentRedirect) + } else { + c.Next() + } + } +} + // InitWebUIRouter initializes the webUI router. func InitWebUIRouter() *gin.Engine { router := gin.New() router.Use(gin.Recovery()) + router.Use(alwaysRedirect(indexPath)) router.Use(openapi.ZapLogger(log.L().WithFields(zap.String("component", "webui")).Logger)) - router.StaticFS("/dashboard/", WebUIAssetsHandler()) + router.StaticFS(indexPath, WebUIAssetsHandler()) return router } From a840fdbacde4f8e121ce32e2c482709b554996f2 Mon Sep 17 00:00:00 2001 From: ehco1996 Date: Mon, 17 Jan 2022 10:06:44 +0800 Subject: [PATCH 2/3] fix comments --- dm/ui/server.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dm/ui/server.go b/dm/ui/server.go index 7c7911e221d..4ad0ff64ad3 100644 --- a/dm/ui/server.go +++ b/dm/ui/server.go @@ -41,10 +41,11 @@ func WebUIAssetsHandler() http.FileSystem { return http.FS(stripped) } -// we need this to handel this case: user want to access /dashboard/source.html/ but webui is a single page app. -// and it only can handel requests in index page, so we need to redirect to index page +// we need this to handel this case: user want to access /dashboard/source.html/ but webui is a single page app, +// and it only can handel requests in index page, so we need to redirect to index page. func alwaysRedirect(path string) gin.HandlerFunc { return func(c *gin.Context) { + // note that static file like css and js under the assets folder should not be redirected. if c.Request.URL.Path != path && !strings.Contains(c.Request.URL.Path, assetsPath) { c.Redirect(http.StatusPermanentRedirect, path) c.AbortWithStatus(http.StatusPermanentRedirect) From 5ec9183871694559088a66b68242ae73ba6d73f0 Mon Sep 17 00:00:00 2001 From: Ehco Date: Wed, 26 Jan 2022 15:16:21 +0800 Subject: [PATCH 3/3] Update dm/ui/server.go Co-authored-by: D3Hunter --- dm/ui/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dm/ui/server.go b/dm/ui/server.go index 4ad0ff64ad3..722a6b7d2fb 100644 --- a/dm/ui/server.go +++ b/dm/ui/server.go @@ -41,8 +41,8 @@ func WebUIAssetsHandler() http.FileSystem { return http.FS(stripped) } -// we need this to handel this case: user want to access /dashboard/source.html/ but webui is a single page app, -// and it only can handel requests in index page, so we need to redirect to index page. +// we need this to handle this case: user want to access /dashboard/source.html/ but webui is a single page app, +// and it only can handle requests in index page, so we need to redirect to index page. func alwaysRedirect(path string) gin.HandlerFunc { return func(c *gin.Context) { // note that static file like css and js under the assets folder should not be redirected.