| 30 | } |
| 31 | |
| 32 | func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 33 | // we already parsed and looked up the cluster in the clusterresolver middleware, |
| 34 | // and potentially have stored the shard URL in the context already |
| 35 | shardURL := lookup.ShardURLFrom(r.Context()) |
| 36 | if shardURL != nil { |
| 37 | r.URL = shardURL |
| 38 | } |
| 39 | |
| 40 | mux := http.NewServeMux() |
| 41 | |
| 42 | // fallback for all unrecognized URLs |
| 43 | if h.DefaultHandler != nil { |
| 44 | mux.Handle("/", h.DefaultHandler) |
| 45 | } |
| 46 | |
| 47 | for _, mapping := range h.Mappings { |
| 48 | p := strings.TrimRight(mapping.Path, "/") |
| 49 | |
| 50 | if p == "/clusters" { |
| 51 | mux.Handle("/clusters/{cluster}", mapping.Handler) |
| 52 | mux.Handle("/clusters/{cluster}/{trail...}", mapping.Handler) |
| 53 | } else { |
| 54 | mux.Handle(p, mapping.Handler) |
| 55 | mux.Handle(p+"/{trail...}", mapping.Handler) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | mux.ServeHTTP(w, r) |
| 60 | } |