Allow BaseURL to contain full server url, including scheme and host. Fix #2183

This commit is contained in:
Deluan
2023-02-15 21:13:38 -05:00
parent aac6e2cb07
commit 10108c63c9
8 changed files with 102 additions and 17 deletions
+17 -9
View File
@@ -19,6 +19,7 @@ import (
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/ui"
. "github.com/navidrome/navidrome/utils/gg"
)
type Server struct {
@@ -38,7 +39,7 @@ func New(ds model.DataStore) *Server {
}
func (s *Server) MountRouter(description, urlPath string, subRouter http.Handler) {
urlPath = path.Join(conf.Server.BaseURL, urlPath)
urlPath = path.Join(conf.Server.BasePath, urlPath)
log.Info(fmt.Sprintf("Mounting %s routes", description), "path", urlPath)
s.router.Group(func(r chi.Router) {
r.Mount(urlPath, subRouter)
@@ -82,7 +83,7 @@ func (s *Server) Run(ctx context.Context, addr string) error {
}
func (s *Server) initRoutes() {
s.appRoot = path.Join(conf.Server.BaseURL, consts.URLPathUI)
s.appRoot = path.Join(conf.Server.BasePath, consts.URLPathUI)
r := chi.NewRouter()
@@ -103,7 +104,7 @@ func (s *Server) initRoutes() {
r.Use(authHeaderMapper)
r.Use(jwtVerifier)
r.Route(path.Join(conf.Server.BaseURL, "/auth"), func(r chi.Router) {
r.Route(path.Join(conf.Server.BasePath, "/auth"), func(r chi.Router) {
if conf.Server.AuthRequestLimit > 0 {
log.Info("Login rate limit set", "requestLimit", conf.Server.AuthRequestLimit,
"windowLength", conf.Server.AuthWindowLength)
@@ -138,13 +139,20 @@ func (s *Server) frontendAssetsHandler() http.Handler {
return r
}
func AbsoluteURL(r *http.Request, url string, params url.Values) string {
if strings.HasPrefix(url, "/") {
appRoot := path.Join(r.Host, conf.Server.BaseURL, url)
url = r.URL.Scheme + "://" + appRoot
func AbsoluteURL(r *http.Request, u string, params url.Values) string {
buildUrl, _ := url.Parse(u)
if strings.HasPrefix(u, "/") {
buildUrl.Path = path.Join(conf.Server.BasePath, buildUrl.Path)
if conf.Server.BaseHost != "" {
buildUrl.Scheme = IfZero(conf.Server.BaseScheme, "http")
buildUrl.Host = conf.Server.BaseHost
} else {
buildUrl.Scheme = r.URL.Scheme
buildUrl.Host = r.Host
}
}
if len(params) > 0 {
url = url + "?" + params.Encode()
buildUrl.RawQuery = params.Encode()
}
return url
return buildUrl.String()
}