Set up prefixes properly

This commit is contained in:
Neil Alexander 2020-05-22 11:25:10 +01:00
parent 9d44836cc3
commit 40f7da6c39
2 changed files with 12 additions and 5 deletions

View file

@ -96,14 +96,15 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, enableHTTPAPIs
logrus.WithError(err).Warnf("Failed to create cache") logrus.WithError(err).Warnf("Failed to create cache")
} }
httpmux := mux.NewRouter()
return &BaseDendrite{ return &BaseDendrite{
componentName: componentName, componentName: componentName,
EnableHTTPAPIs: enableHTTPAPIs, EnableHTTPAPIs: enableHTTPAPIs,
tracerCloser: closer, tracerCloser: closer,
Cfg: cfg, Cfg: cfg,
ImmutableCache: cache, ImmutableCache: cache,
PublicAPIMux: mux.NewRouter().UseEncodedPath(), PublicAPIMux: httpmux.PathPrefix(internal.HTTPPublicPathPrefix).Subrouter().UseEncodedPath(),
InternalAPIMux: mux.NewRouter().UseEncodedPath(), InternalAPIMux: httpmux.PathPrefix(internal.HTTPInternalPathPrefix).Subrouter().UseEncodedPath(),
httpClient: &http.Client{Timeout: HTTPClientTimeout}, httpClient: &http.Client{Timeout: HTTPClientTimeout},
KafkaConsumer: kafkaConsumer, KafkaConsumer: kafkaConsumer,
KafkaProducer: kafkaProducer, KafkaProducer: kafkaProducer,

View file

@ -9,6 +9,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/gorilla/mux"
"github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/internal/config"
@ -22,6 +23,11 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const (
HTTPPublicPathPrefix = "/_matrix/"
HTTPInternalPathPrefix = "/api/"
)
// BasicAuth is used for authorization on /metrics handlers // BasicAuth is used for authorization on /metrics handlers
type BasicAuth struct { type BasicAuth struct {
Username string `yaml:"username"` Username string `yaml:"username"`
@ -184,14 +190,14 @@ func MakeFedAPI(
// SetupHTTPAPI registers an HTTP API mux under /api and sets up a metrics // SetupHTTPAPI registers an HTTP API mux under /api and sets up a metrics
// listener. // 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 { if cfg.Metrics.Enabled {
servMux.Handle("/metrics", WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Metrics.BasicAuth)) servMux.Handle("/metrics", WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Metrics.BasicAuth))
} }
if enableHTTPAPIs { 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 // WrapHandlerInBasicAuth adds basic auth to a handler. Only used for /metrics