From 40f7da6c3980e45bc8492a2843ee8b957f6e27af Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 22 May 2020 11:25:10 +0100 Subject: [PATCH] Set up prefixes properly --- internal/basecomponent/base.go | 5 +++-- internal/httpapi.go | 12 +++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/basecomponent/base.go b/internal/basecomponent/base.go index f75faf044..0682ae0ea 100644 --- a/internal/basecomponent/base.go +++ b/internal/basecomponent/base.go @@ -96,14 +96,15 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, enableHTTPAPIs logrus.WithError(err).Warnf("Failed to create cache") } + httpmux := mux.NewRouter() return &BaseDendrite{ componentName: componentName, EnableHTTPAPIs: enableHTTPAPIs, tracerCloser: closer, Cfg: cfg, ImmutableCache: cache, - PublicAPIMux: mux.NewRouter().UseEncodedPath(), - InternalAPIMux: mux.NewRouter().UseEncodedPath(), + PublicAPIMux: httpmux.PathPrefix(internal.HTTPPublicPathPrefix).Subrouter().UseEncodedPath(), + InternalAPIMux: httpmux.PathPrefix(internal.HTTPInternalPathPrefix).Subrouter().UseEncodedPath(), httpClient: &http.Client{Timeout: HTTPClientTimeout}, KafkaConsumer: kafkaConsumer, KafkaProducer: kafkaProducer, diff --git a/internal/httpapi.go b/internal/httpapi.go index 0d50ff932..526ff8a2d 100644 --- a/internal/httpapi.go +++ b/internal/httpapi.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/internal/config" @@ -22,6 +23,11 @@ import ( "github.com/sirupsen/logrus" ) +const ( + HTTPPublicPathPrefix = "/_matrix/" + HTTPInternalPathPrefix = "/api/" +) + // BasicAuth is used for authorization on /metrics handlers type BasicAuth struct { Username string `yaml:"username"` @@ -184,14 +190,14 @@ func MakeFedAPI( // SetupHTTPAPI registers an HTTP API mux under /api and sets up a metrics // listener. -func SetupHTTPAPI(servMux *http.ServeMux, publicApiMux http.Handler, internalApiMux http.Handler, cfg *config.Dendrite, enableHTTPAPIs bool) { +func SetupHTTPAPI(servMux *http.ServeMux, publicApiMux *mux.Router, internalApiMux *mux.Router, cfg *config.Dendrite, enableHTTPAPIs bool) { if cfg.Metrics.Enabled { servMux.Handle("/metrics", WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Metrics.BasicAuth)) } if enableHTTPAPIs { - servMux.Handle("/api", internalApiMux) + servMux.Handle(HTTPInternalPathPrefix, internalApiMux) } - servMux.Handle("/_matrix", WrapHandlerInCORS(publicApiMux)) + servMux.Handle(HTTPPublicPathPrefix, WrapHandlerInCORS(publicApiMux)) } // WrapHandlerInBasicAuth adds basic auth to a handler. Only used for /metrics