Skip to content

Commit ecf31e9

Browse files
committed
internal/relui: correct baseURL behavior
Nested http.ServeMux should be configured with the appropriate trailing slash, but we should not use the trailing slash when using http.StripPrefix. This simplifies ServeHTTP slightly, and corrects behavior when handling nested mux. For golang/go#47401 Change-Id: I2e6ab792e210a500025b8b07a28e3db67696397d Reviewed-on: https://go-review.googlesource.com/c/build/+/363977 Trust: Alexander Rakoczy <[email protected]> Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 4d86312 commit ecf31e9

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

internal/relui/web.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"net/http"
1818
"net/url"
1919
"path"
20+
"strings"
2021

2122
"github.com/google/uuid"
2223
"github.com/jackc/pgx/v4/pgxpool"
@@ -48,6 +49,8 @@ type Server struct {
4849
m *http.ServeMux
4950
w *Worker
5051
baseURL *url.URL
52+
// mux used if baseURL is set
53+
bm *http.ServeMux
5154

5255
homeTmpl *template.Template
5356
newWorkflowTmpl *template.Template
@@ -70,19 +73,21 @@ func NewServer(p *pgxpool.Pool, w *Worker, baseURL *url.URL) *Server {
7073
s.m.Handle("/workflows/create", http.HandlerFunc(s.createWorkflowHandler))
7174
s.m.Handle("/workflows/new", http.HandlerFunc(s.newWorkflowHandler))
7275
s.m.Handle("/", fileServerHandler(static, http.HandlerFunc(s.homeHandler)))
76+
if baseURL != nil && baseURL.Path != "/" && baseURL.Path != "" {
77+
nosuffix := strings.TrimSuffix(baseURL.Path, "/")
78+
s.bm = new(http.ServeMux)
79+
s.bm.Handle(nosuffix+"/", http.StripPrefix(nosuffix, s.m))
80+
s.bm.Handle("/", s.m)
81+
}
7382
return s
7483
}
7584

7685
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
77-
if s.baseURL == nil || s.baseURL.Path == "/" {
78-
s.m.ServeHTTP(w, r)
86+
if s.bm != nil {
87+
s.bm.ServeHTTP(w, r)
7988
return
8089
}
81-
http.StripPrefix(s.baseURL.Path, s.m)
82-
}
83-
84-
func (s *Server) Serve(port string) error {
85-
return http.ListenAndServe(":"+port, s.m)
90+
s.m.ServeHTTP(w, r)
8691
}
8792

8893
func (s *Server) BaseLink(target string) string {

0 commit comments

Comments
 (0)