Define path prefixes in a package that doesn't create import cycles
MODIFY
internal/basecomponent/base.go
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,

MODIFY
internal/http/http.go
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

MODIFY
internal/httpapi.go
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

ADD
internal/httpapis/paths.go
+package httpapis
+
+const (
+ PublicPathPrefix = "/_matrix/"
+ InternalPathPrefix = "/api/"
+)

Define path prefixes in a package that doesn't create import cycles