Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webui(dm): always redircet to index page #4350

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}