From d7db9e4efe508677c50f539e0aa7bba4a25a33df Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Tue, 7 Jun 2022 15:47:21 +0200 Subject: [PATCH] Add trailing slash for routed directories (datarhei/restreamer#340) In order for the UI to work properly with a relative %PUBLIC_URL% the route for the UI requires a / at the end. This commit is enforcing this. As a consequence, if the UI is behind a reverse proxy, it will still load properly. --- http/server.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/http/server.go b/http/server.go index 2cb7765b..8c88a646 100644 --- a/http/server.go +++ b/http/server.go @@ -338,13 +338,19 @@ func NewServer(config Config) (Server, error) { s.router.Use(s.middleware.cors) } - s.router.Use(middleware.RemoveTrailingSlashWithConfig(middleware.TrailingSlashConfig{ - RedirectCode: 301, - })) - // Add static routes if path, target := config.Router.StaticRoute(); len(target) != 0 { group := s.router.Group(path) + group.Use(middleware.AddTrailingSlashWithConfig(middleware.TrailingSlashConfig{ + Skipper: func(c echo.Context) bool { + if path == c.Request().URL.Path { + return false + } + + return true + }, + RedirectCode: 301, + })) group.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Skipper: middleware.DefaultSkipper, Root: target, @@ -359,6 +365,18 @@ func NewServer(config Config) (Server, error) { for prefix, target := range config.Router.DirRoutes() { group := s.router.Group(prefix) + group.Use(middleware.AddTrailingSlashWithConfig(middleware.TrailingSlashConfig{ + Skipper: func(prefix string) func(c echo.Context) bool { + return func(c echo.Context) bool { + if prefix == c.Request().URL.Path { + return false + } + + return true + } + }(prefix), + RedirectCode: 301, + })) group.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Skipper: middleware.DefaultSkipper, Root: target,