Skip to content

Commit

Permalink
Merge branch 'master' into fix_4248_owner_exit
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesCheung96 authored Jan 27, 2022
2 parents 29dd9cb + 757decd commit bf43014
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: 2
updates:
- package-ecosystem: "npm"
directory: "/dm/ui"
schedule:
interval: "weekly"
# Specify labels for npm pull requests.
labels:
- "dependencies"
Expand Down
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
}
6 changes: 3 additions & 3 deletions dm/ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3167,9 +3167,9 @@ node-fetch-h2@^2.3.0:
http2-client "^1.2.5"

node-fetch@^2.6.1:
version "2.6.6"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.6.tgz#1751a7c01834e8e1697758732e9efb6eeadfaf89"
integrity sha512-Z8/6vRlTUChSdIgMa51jxQ4lrw/Jy5SOW10ObaA47/RElsAN2c5Pn8bTgFGWn/ibwzXTE8qwr1Yzx28vsecXEA==
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

Expand Down

0 comments on commit bf43014

Please sign in to comment.