From 3c3e014901fd03ea46ff4c5dfa125c6aa148d5ca Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 22 May 2020 14:18:41 +0100 Subject: [PATCH] Define path prefixes in a package that doesn't create import cycles --- internal/basecomponent/base.go | 5 +++-- internal/http/http.go | 11 +++++++++++ internal/httpapi.go | 10 +++------- internal/httpapis/paths.go | 6 ++++++ 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 internal/httpapis/paths.go diff --git a/internal/basecomponent/base.go b/internal/basecomponent/base.go index 45aa454a..3781cea4 100644 --- a/internal/basecomponent/base.go +++ b/internal/basecomponent/base.go @@ -24,6 +24,7 @@ import ( "golang.org/x/crypto/ed25519" "github.com/matrix-org/dendrite/internal/caching" + "github.com/matrix-org/dendrite/internal/httpapis" "github.com/matrix-org/dendrite/internal/keydb" "github.com/matrix-org/dendrite/internal/keydb/cache" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -103,8 +104,8 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, enableHTTPAPIs tracerCloser: closer, Cfg: cfg, ImmutableCache: cache, - PublicAPIMux: httpmux.PathPrefix(internal.HTTPPublicPathPrefix).Subrouter().UseEncodedPath(), - InternalAPIMux: httpmux.PathPrefix(internal.HTTPInternalPathPrefix).Subrouter().UseEncodedPath(), + PublicAPIMux: httpmux.PathPrefix(httpapis.PublicPathPrefix).Subrouter().UseEncodedPath(), + InternalAPIMux: httpmux.PathPrefix(httpapis.InternalPathPrefix).Subrouter().UseEncodedPath(), httpClient: &http.Client{Timeout: HTTPClientTimeout}, KafkaConsumer: kafkaConsumer, KafkaProducer: kafkaProducer, diff --git a/internal/http/http.go b/internal/http/http.go index 77896a53..da90b375 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -6,7 +6,10 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" + "strings" + "github.com/matrix-org/dendrite/internal/httpapis" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" ) @@ -21,6 +24,14 @@ func PostJSON( return err } + parsedAPIURL, err := url.Parse(apiURL) + if err != nil { + return err + } + + parsedAPIURL.Path = httpapis.InternalPathPrefix + strings.TrimLeft(parsedAPIURL.Path, "/") + apiURL = parsedAPIURL.String() + req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewReader(jsonBytes)) if err != nil { return err diff --git a/internal/httpapi.go b/internal/httpapi.go index 526ff8a2..07bbacdd 100644 --- a/internal/httpapi.go +++ b/internal/httpapi.go @@ -13,6 +13,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/internal/config" + "github.com/matrix-org/dendrite/internal/httpapis" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" opentracing "github.com/opentracing/opentracing-go" @@ -23,11 +24,6 @@ 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"` @@ -195,9 +191,9 @@ func SetupHTTPAPI(servMux *http.ServeMux, publicApiMux *mux.Router, internalApiM servMux.Handle("/metrics", WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Metrics.BasicAuth)) } if enableHTTPAPIs { - servMux.Handle(HTTPInternalPathPrefix, internalApiMux) + servMux.Handle(httpapis.InternalPathPrefix, internalApiMux) } - servMux.Handle(HTTPPublicPathPrefix, WrapHandlerInCORS(publicApiMux)) + servMux.Handle(httpapis.PublicPathPrefix, WrapHandlerInCORS(publicApiMux)) } // WrapHandlerInBasicAuth adds basic auth to a handler. Only used for /metrics diff --git a/internal/httpapis/paths.go b/internal/httpapis/paths.go new file mode 100644 index 00000000..8adec2df --- /dev/null +++ b/internal/httpapis/paths.go @@ -0,0 +1,6 @@ +package httpapis + +const ( + PublicPathPrefix = "/_matrix/" + InternalPathPrefix = "/api/" +)