Skip to content

Commit

Permalink
webui(dm): always redircet to index page (#4350)
Browse files Browse the repository at this point in the history
ref #3583
  • Loading branch information
Ehco1996 authored Jan 27, 2022
1 parent 650d748 commit 757decd
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions dm/ui/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,49 @@ package ui
import (
"io/fs"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/pingcap/tiflow/dm/openapi"
"github.com/pingcap/tiflow/dm/pkg/log"
"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 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.
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
}

0 comments on commit 757decd

Please sign in to comment.