2017-11-09 15:12:04 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-07-28 08:08:03 +00:00
|
|
|
"net/url"
|
2017-11-09 15:12:04 +00:00
|
|
|
|
2019-02-18 06:52:03 +00:00
|
|
|
assetfs "github.com/elazarl/go-bindata-assetfs"
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/gorilla/mux"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2017-11-09 15:12:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// DashboardHandler expose dashboard routes.
|
2018-05-25 13:10:04 +00:00
|
|
|
type DashboardHandler struct {
|
|
|
|
Assets *assetfs.AssetFS
|
|
|
|
}
|
2017-11-09 15:12:04 +00:00
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// Append add dashboard routes on a router.
|
2018-11-14 09:18:03 +00:00
|
|
|
func (g DashboardHandler) Append(router *mux.Router) {
|
2018-05-25 13:10:04 +00:00
|
|
|
if g.Assets == nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
log.WithoutContext().Error("No assets for dashboard")
|
2018-05-25 13:10:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-09 15:12:04 +00:00
|
|
|
// Expose dashboard
|
2018-04-27 11:12:04 +00:00
|
|
|
router.Methods(http.MethodGet).
|
|
|
|
Path("/").
|
2020-07-28 08:08:03 +00:00
|
|
|
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
http.Redirect(resp, req, safePrefix(req)+"/dashboard/", http.StatusFound)
|
2018-04-27 11:12:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.Methods(http.MethodGet).
|
|
|
|
PathPrefix("/dashboard/").
|
2018-05-25 13:10:04 +00:00
|
|
|
Handler(http.StripPrefix("/dashboard/", http.FileServer(g.Assets)))
|
2017-11-09 15:12:04 +00:00
|
|
|
}
|
2020-07-28 08:08:03 +00:00
|
|
|
|
|
|
|
func safePrefix(req *http.Request) string {
|
|
|
|
prefix := req.Header.Get("X-Forwarded-Prefix")
|
|
|
|
if prefix == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
parse, err := url.Parse(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if parse.Host != "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parse.Path
|
|
|
|
}
|