From dd061a172e97005a4a7a4c37db6caf3f77c10d51 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 3 May 2022 17:17:02 +0100 Subject: [PATCH] Tidy up `AddPublicRoutes` (#2412) * Simplify federation API `AddPublicRoutes` * Simplify client API `AddPublicRoutes` * Simplify media API `AddPublicRoutes` * Simplify sync API `AddPublicRoutes` * Simplify `AddAllPublicRoutes` --- build/gobind-pinecone/monolith.go | 11 +---------- build/gobind-yggdrasil/monolith.go | 11 +---------- clientapi/clientapi.go | 19 ++++++++----------- cmd/dendrite-demo-pinecone/main.go | 11 +---------- cmd/dendrite-demo-yggdrasil/main.go | 11 +---------- cmd/dendrite-monolith-server/main.go | 11 +---------- .../personalities/clientapi.go | 6 ++---- .../personalities/federationapi.go | 7 +++---- .../personalities/mediaapi.go | 3 +-- .../personalities/syncapi.go | 4 ++-- cmd/dendritejs-pinecone/main.go | 11 +---------- federationapi/federationapi.go | 19 +++++++++---------- federationapi/federationapi_test.go | 2 +- mediaapi/mediaapi.go | 10 ++++------ setup/monolith.go | 19 +++++++------------ syncapi/syncapi.go | 11 ++++++----- 16 files changed, 49 insertions(+), 117 deletions(-) diff --git a/build/gobind-pinecone/monolith.go b/build/gobind-pinecone/monolith.go index 8cf663d00..310ac7dda 100644 --- a/build/gobind-pinecone/monolith.go +++ b/build/gobind-pinecone/monolith.go @@ -306,16 +306,7 @@ func (m *DendriteMonolith) Start() { ExtPublicRoomsProvider: roomProvider, ExtUserDirectoryProvider: userProvider, } - monolith.AddAllPublicRoutes( - base, - base.PublicClientAPIMux, - base.PublicFederationAPIMux, - base.PublicKeyAPIMux, - base.PublicWellKnownAPIMux, - base.PublicMediaAPIMux, - base.SynapseAdminMux, - base.DendriteAdminMux, - ) + monolith.AddAllPublicRoutes(base) httpRouter := mux.NewRouter().SkipClean(true).UseEncodedPath() httpRouter.PathPrefix(httputil.InternalPathPrefix).Handler(base.InternalAPIMux) diff --git a/build/gobind-yggdrasil/monolith.go b/build/gobind-yggdrasil/monolith.go index 2c7d4e91b..991bc462f 100644 --- a/build/gobind-yggdrasil/monolith.go +++ b/build/gobind-yggdrasil/monolith.go @@ -144,16 +144,7 @@ func (m *DendriteMonolith) Start() { ygg, fsAPI, federation, ), } - monolith.AddAllPublicRoutes( - base, - base.PublicClientAPIMux, - base.PublicFederationAPIMux, - base.PublicKeyAPIMux, - base.PublicWellKnownAPIMux, - base.PublicMediaAPIMux, - base.SynapseAdminMux, - base.DendriteAdminMux, - ) + monolith.AddAllPublicRoutes(base) httpRouter := mux.NewRouter() httpRouter.PathPrefix(httputil.InternalPathPrefix).Handler(base.InternalAPIMux) diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index ad277056c..0d16e4c14 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -15,7 +15,6 @@ package clientapi import ( - "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/api" "github.com/matrix-org/dendrite/clientapi/producers" @@ -24,20 +23,15 @@ import ( "github.com/matrix-org/dendrite/internal/transactions" keyserverAPI "github.com/matrix-org/dendrite/keyserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" - "github.com/matrix-org/dendrite/setup/config" + "github.com/matrix-org/dendrite/setup/base" "github.com/matrix-org/dendrite/setup/jetstream" - "github.com/matrix-org/dendrite/setup/process" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" ) // AddPublicRoutes sets up and registers HTTP handlers for the ClientAPI component. func AddPublicRoutes( - process *process.ProcessContext, - router *mux.Router, - synapseAdminRouter *mux.Router, - dendriteAdminRouter *mux.Router, - cfg *config.ClientAPI, + base *base.BaseDendrite, federation *gomatrixserverlib.FederationClient, rsAPI roomserverAPI.RoomserverInternalAPI, asAPI appserviceAPI.AppServiceQueryAPI, @@ -47,9 +41,10 @@ func AddPublicRoutes( userDirectoryProvider userapi.UserDirectoryProvider, keyAPI keyserverAPI.KeyInternalAPI, extRoomsProvider api.ExtraPublicRoomsProvider, - mscCfg *config.MSCs, ) { - js, natsClient := jetstream.Prepare(process, &cfg.Matrix.JetStream) + cfg := &base.Cfg.ClientAPI + mscCfg := &base.Cfg.MSCs + js, natsClient := jetstream.Prepare(base.ProcessContext, &cfg.Matrix.JetStream) syncProducer := &producers.SyncAPIProducer{ JetStream: js, @@ -63,7 +58,9 @@ func AddPublicRoutes( } routing.Setup( - router, synapseAdminRouter, dendriteAdminRouter, + base.PublicClientAPIMux, + base.SynapseAdminMux, + base.DendriteAdminMux, cfg, rsAPI, asAPI, userAPI, userDirectoryProvider, federation, syncProducer, transactionsCache, fsAPI, keyAPI, diff --git a/cmd/dendrite-demo-pinecone/main.go b/cmd/dendrite-demo-pinecone/main.go index 33487e64b..703436051 100644 --- a/cmd/dendrite-demo-pinecone/main.go +++ b/cmd/dendrite-demo-pinecone/main.go @@ -185,16 +185,7 @@ func main() { ExtPublicRoomsProvider: roomProvider, ExtUserDirectoryProvider: userProvider, } - monolith.AddAllPublicRoutes( - base, - base.PublicClientAPIMux, - base.PublicFederationAPIMux, - base.PublicKeyAPIMux, - base.PublicWellKnownAPIMux, - base.PublicMediaAPIMux, - base.SynapseAdminMux, - base.DendriteAdminMux, - ) + monolith.AddAllPublicRoutes(base) wsUpgrader := websocket.Upgrader{ CheckOrigin: func(_ *http.Request) bool { diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index df9ba5121..619720d6c 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -142,16 +142,7 @@ func main() { ygg, fsAPI, federation, ), } - monolith.AddAllPublicRoutes( - base, - base.PublicClientAPIMux, - base.PublicFederationAPIMux, - base.PublicKeyAPIMux, - base.PublicWellKnownAPIMux, - base.PublicMediaAPIMux, - base.SynapseAdminMux, - base.DendriteAdminMux, - ) + monolith.AddAllPublicRoutes(base) if err := mscs.Enable(base, &monolith); err != nil { logrus.WithError(err).Fatalf("Failed to enable MSCs") } diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 4c7c4297f..2fa4675a4 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -143,16 +143,7 @@ func main() { UserAPI: userAPI, KeyAPI: keyAPI, } - monolith.AddAllPublicRoutes( - base, - base.PublicClientAPIMux, - base.PublicFederationAPIMux, - base.PublicKeyAPIMux, - base.PublicWellKnownAPIMux, - base.PublicMediaAPIMux, - base.SynapseAdminMux, - base.DendriteAdminMux, - ) + monolith.AddAllPublicRoutes(base) if len(base.Cfg.MSCs.MSCs) > 0 { if err := mscs.Enable(base, &monolith); err != nil { diff --git a/cmd/dendrite-polylith-multi/personalities/clientapi.go b/cmd/dendrite-polylith-multi/personalities/clientapi.go index 7ed2075aa..a5d69d07c 100644 --- a/cmd/dendrite-polylith-multi/personalities/clientapi.go +++ b/cmd/dendrite-polylith-multi/personalities/clientapi.go @@ -31,11 +31,9 @@ func ClientAPI(base *basepkg.BaseDendrite, cfg *config.Dendrite) { keyAPI := base.KeyServerHTTPClient() clientapi.AddPublicRoutes( - base.ProcessContext, base.PublicClientAPIMux, - base.SynapseAdminMux, base.DendriteAdminMux, - &base.Cfg.ClientAPI, federation, rsAPI, asQuery, + base, federation, rsAPI, asQuery, transactions.New(), fsAPI, userAPI, userAPI, - keyAPI, nil, &cfg.MSCs, + keyAPI, nil, ) base.SetupAndServeHTTP( diff --git a/cmd/dendrite-polylith-multi/personalities/federationapi.go b/cmd/dendrite-polylith-multi/personalities/federationapi.go index b82577ce3..6377ce9e3 100644 --- a/cmd/dendrite-polylith-multi/personalities/federationapi.go +++ b/cmd/dendrite-polylith-multi/personalities/federationapi.go @@ -29,10 +29,9 @@ func FederationAPI(base *basepkg.BaseDendrite, cfg *config.Dendrite) { keyRing := fsAPI.KeyRing() federationapi.AddPublicRoutes( - base.ProcessContext, base.PublicFederationAPIMux, base.PublicKeyAPIMux, base.PublicWellKnownAPIMux, - &base.Cfg.FederationAPI, userAPI, federation, keyRing, - rsAPI, fsAPI, keyAPI, - &base.Cfg.MSCs, nil, + base, + userAPI, federation, keyRing, + rsAPI, fsAPI, keyAPI, nil, ) federationapi.AddInternalRoutes(base.InternalAPIMux, fsAPI) diff --git a/cmd/dendrite-polylith-multi/personalities/mediaapi.go b/cmd/dendrite-polylith-multi/personalities/mediaapi.go index 8c0bfa195..69d5fd5a8 100644 --- a/cmd/dendrite-polylith-multi/personalities/mediaapi.go +++ b/cmd/dendrite-polylith-multi/personalities/mediaapi.go @@ -25,8 +25,7 @@ func MediaAPI(base *basepkg.BaseDendrite, cfg *config.Dendrite) { client := base.CreateClient() mediaapi.AddPublicRoutes( - base, base.PublicMediaAPIMux, &base.Cfg.MediaAPI, &base.Cfg.ClientAPI.RateLimiting, - userAPI, client, + base, userAPI, client, ) base.SetupAndServeHTTP( diff --git a/cmd/dendrite-polylith-multi/personalities/syncapi.go b/cmd/dendrite-polylith-multi/personalities/syncapi.go index f9f1c5a09..2245b9b54 100644 --- a/cmd/dendrite-polylith-multi/personalities/syncapi.go +++ b/cmd/dendrite-polylith-multi/personalities/syncapi.go @@ -28,9 +28,9 @@ func SyncAPI(base *basepkg.BaseDendrite, cfg *config.Dendrite) { syncapi.AddPublicRoutes( base, - base.PublicClientAPIMux, userAPI, rsAPI, + userAPI, rsAPI, base.KeyServerHTTPClient(), - federation, &cfg.SyncAPI, + federation, ) base.SetupAndServeHTTP( diff --git a/cmd/dendritejs-pinecone/main.go b/cmd/dendritejs-pinecone/main.go index ead381368..e070173aa 100644 --- a/cmd/dendritejs-pinecone/main.go +++ b/cmd/dendritejs-pinecone/main.go @@ -212,16 +212,7 @@ func startup() { //ServerKeyAPI: serverKeyAPI, ExtPublicRoomsProvider: rooms.NewPineconeRoomProvider(pRouter, pSessions, fedSenderAPI, federation), } - monolith.AddAllPublicRoutes( - base, - base.PublicClientAPIMux, - base.PublicFederationAPIMux, - base.PublicKeyAPIMux, - base.PublicWellKnownAPIMux, - base.PublicMediaAPIMux, - base.SynapseAdminMux, - base.DendriteAdminMux, - ) + monolith.AddAllPublicRoutes(base) httpRouter := mux.NewRouter().SkipClean(true).UseEncodedPath() httpRouter.PathPrefix(httputil.InternalPathPrefix).Handler(base.InternalAPIMux) diff --git a/federationapi/federationapi.go b/federationapi/federationapi.go index 1848a242e..c627aab5c 100644 --- a/federationapi/federationapi.go +++ b/federationapi/federationapi.go @@ -29,9 +29,7 @@ import ( keyserverAPI "github.com/matrix-org/dendrite/keyserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/base" - "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" - "github.com/matrix-org/dendrite/setup/process" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/sirupsen/logrus" @@ -47,20 +45,18 @@ func AddInternalRoutes(router *mux.Router, intAPI api.FederationInternalAPI) { // AddPublicRoutes sets up and registers HTTP handlers on the base API muxes for the FederationAPI component. func AddPublicRoutes( - process *process.ProcessContext, - fedRouter, keyRouter, wellKnownRouter *mux.Router, - cfg *config.FederationAPI, + base *base.BaseDendrite, userAPI userapi.UserInternalAPI, federation *gomatrixserverlib.FederationClient, keyRing gomatrixserverlib.JSONVerifier, rsAPI roomserverAPI.RoomserverInternalAPI, federationAPI federationAPI.FederationInternalAPI, keyAPI keyserverAPI.KeyInternalAPI, - mscCfg *config.MSCs, servers federationAPI.ServersInRoomProvider, ) { - - js, _ := jetstream.Prepare(process, &cfg.Matrix.JetStream) + cfg := &base.Cfg.FederationAPI + mscCfg := &base.Cfg.MSCs + js, _ := jetstream.Prepare(base.ProcessContext, &cfg.Matrix.JetStream) producer := &producers.SyncAPIProducer{ JetStream: js, TopicReceiptEvent: cfg.Matrix.JetStream.Prefixed(jetstream.OutputReceiptEvent), @@ -72,8 +68,11 @@ func AddPublicRoutes( } routing.Setup( - fedRouter, keyRouter, wellKnownRouter, cfg, rsAPI, - federationAPI, keyRing, + base.PublicFederationAPIMux, + base.PublicKeyAPIMux, + base.PublicWellKnownAPIMux, + cfg, + rsAPI, federationAPI, keyRing, federation, userAPI, keyAPI, mscCfg, servers, producer, ) diff --git a/federationapi/federationapi_test.go b/federationapi/federationapi_test.go index 833359c11..687241646 100644 --- a/federationapi/federationapi_test.go +++ b/federationapi/federationapi_test.go @@ -30,7 +30,7 @@ func TestRoomsV3URLEscapeDoNot404(t *testing.T) { fsAPI := base.FederationAPIHTTPClient() // TODO: This is pretty fragile, as if anything calls anything on these nils this test will break. // Unfortunately, it makes little sense to instantiate these dependencies when we just want to test routing. - federationapi.AddPublicRoutes(base.ProcessContext, base.PublicFederationAPIMux, base.PublicKeyAPIMux, base.PublicWellKnownAPIMux, &cfg.FederationAPI, nil, nil, keyRing, nil, fsAPI, nil, &cfg.MSCs, nil) + federationapi.AddPublicRoutes(base, nil, nil, keyRing, nil, fsAPI, nil, nil) baseURL, cancel := test.ListenAndServe(t, base.PublicFederationAPIMux, true) defer cancel() serverName := gomatrixserverlib.ServerName(strings.TrimPrefix(baseURL, "https://")) diff --git a/mediaapi/mediaapi.go b/mediaapi/mediaapi.go index f2fa14384..5976957ca 100644 --- a/mediaapi/mediaapi.go +++ b/mediaapi/mediaapi.go @@ -15,11 +15,9 @@ package mediaapi import ( - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/mediaapi/routing" "github.com/matrix-org/dendrite/mediaapi/storage" "github.com/matrix-org/dendrite/setup/base" - "github.com/matrix-org/dendrite/setup/config" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" @@ -28,18 +26,18 @@ import ( // AddPublicRoutes sets up and registers HTTP handlers for the MediaAPI component. func AddPublicRoutes( base *base.BaseDendrite, - router *mux.Router, - cfg *config.MediaAPI, - rateLimit *config.RateLimiting, userAPI userapi.UserInternalAPI, client *gomatrixserverlib.Client, ) { + cfg := &base.Cfg.MediaAPI + rateCfg := &base.Cfg.ClientAPI.RateLimiting + mediaDB, err := storage.NewMediaAPIDatasource(base, &cfg.Database) if err != nil { logrus.WithError(err).Panicf("failed to connect to media db") } routing.Setup( - router, cfg, rateLimit, mediaDB, userAPI, client, + base.PublicMediaAPIMux, cfg, rateCfg, mediaDB, userAPI, client, ) } diff --git a/setup/monolith.go b/setup/monolith.go index a414172ce..23bd2fb52 100644 --- a/setup/monolith.go +++ b/setup/monolith.go @@ -15,7 +15,6 @@ package setup import ( - "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi" "github.com/matrix-org/dendrite/clientapi/api" @@ -52,28 +51,24 @@ type Monolith struct { } // AddAllPublicRoutes attaches all public paths to the given router -func (m *Monolith) AddAllPublicRoutes(base *base.BaseDendrite, csMux, ssMux, keyMux, wkMux, mediaMux, synapseMux, dendriteMux *mux.Router) { +func (m *Monolith) AddAllPublicRoutes(base *base.BaseDendrite) { userDirectoryProvider := m.ExtUserDirectoryProvider if userDirectoryProvider == nil { userDirectoryProvider = m.UserAPI } clientapi.AddPublicRoutes( - base.ProcessContext, csMux, synapseMux, dendriteMux, &m.Config.ClientAPI, - m.FedClient, m.RoomserverAPI, m.AppserviceAPI, transactions.New(), + base, m.FedClient, m.RoomserverAPI, m.AppserviceAPI, transactions.New(), m.FederationAPI, m.UserAPI, userDirectoryProvider, m.KeyAPI, - m.ExtPublicRoomsProvider, &m.Config.MSCs, + m.ExtPublicRoomsProvider, ) federationapi.AddPublicRoutes( - base.ProcessContext, ssMux, keyMux, wkMux, &m.Config.FederationAPI, - m.UserAPI, m.FedClient, m.KeyRing, m.RoomserverAPI, m.FederationAPI, - m.KeyAPI, &m.Config.MSCs, nil, + base, m.UserAPI, m.FedClient, m.KeyRing, m.RoomserverAPI, m.FederationAPI, + m.KeyAPI, nil, ) mediaapi.AddPublicRoutes( - base, mediaMux, &m.Config.MediaAPI, &m.Config.ClientAPI.RateLimiting, - m.UserAPI, m.Client, + base, m.UserAPI, m.Client, ) syncapi.AddPublicRoutes( - base, csMux, m.UserAPI, m.RoomserverAPI, - m.KeyAPI, m.FedClient, &m.Config.SyncAPI, + base, m.UserAPI, m.RoomserverAPI, m.KeyAPI, m.FedClient, ) } diff --git a/syncapi/syncapi.go b/syncapi/syncapi.go index a2b8859cd..d8becb6ed 100644 --- a/syncapi/syncapi.go +++ b/syncapi/syncapi.go @@ -17,14 +17,12 @@ package syncapi import ( "context" - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/internal/caching" "github.com/sirupsen/logrus" keyapi "github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/base" - "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" @@ -42,13 +40,13 @@ import ( // component. func AddPublicRoutes( base *base.BaseDendrite, - router *mux.Router, userAPI userapi.UserInternalAPI, rsAPI api.RoomserverInternalAPI, keyAPI keyapi.KeyInternalAPI, federation *gomatrixserverlib.FederationClient, - cfg *config.SyncAPI, ) { + cfg := &base.Cfg.SyncAPI + js, natsClient := jetstream.Prepare(base.ProcessContext, &cfg.Matrix.JetStream) syncDB, err := storage.NewSyncServerDatasource(base, &cfg.Database) @@ -148,5 +146,8 @@ func AddPublicRoutes( logrus.WithError(err).Panicf("failed to start presence consumer") } - routing.Setup(router, requestPool, syncDB, userAPI, federation, rsAPI, cfg, lazyLoadCache) + routing.Setup( + base.PublicClientAPIMux, requestPool, syncDB, userAPI, + federation, rsAPI, cfg, lazyLoadCache, + ) }