Skip to content

Commit d7db9e4

Browse files
committed
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.
1 parent 9746248 commit d7db9e4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

http/server.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,19 @@ func NewServer(config Config) (Server, error) {
338338
s.router.Use(s.middleware.cors)
339339
}
340340

341-
s.router.Use(middleware.RemoveTrailingSlashWithConfig(middleware.TrailingSlashConfig{
342-
RedirectCode: 301,
343-
}))
344-
345341
// Add static routes
346342
if path, target := config.Router.StaticRoute(); len(target) != 0 {
347343
group := s.router.Group(path)
344+
group.Use(middleware.AddTrailingSlashWithConfig(middleware.TrailingSlashConfig{
345+
Skipper: func(c echo.Context) bool {
346+
if path == c.Request().URL.Path {
347+
return false
348+
}
349+
350+
return true
351+
},
352+
RedirectCode: 301,
353+
}))
348354
group.Use(middleware.StaticWithConfig(middleware.StaticConfig{
349355
Skipper: middleware.DefaultSkipper,
350356
Root: target,
@@ -359,6 +365,18 @@ func NewServer(config Config) (Server, error) {
359365

360366
for prefix, target := range config.Router.DirRoutes() {
361367
group := s.router.Group(prefix)
368+
group.Use(middleware.AddTrailingSlashWithConfig(middleware.TrailingSlashConfig{
369+
Skipper: func(prefix string) func(c echo.Context) bool {
370+
return func(c echo.Context) bool {
371+
if prefix == c.Request().URL.Path {
372+
return false
373+
}
374+
375+
return true
376+
}
377+
}(prefix),
378+
RedirectCode: 301,
379+
}))
362380
group.Use(middleware.StaticWithConfig(middleware.StaticConfig{
363381
Skipper: middleware.DefaultSkipper,
364382
Root: target,

0 commit comments

Comments
 (0)