From 4f171c56a832c836b0eb21650ee84d56e451dd6a Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 8 Jun 2020 15:51:07 +0100 Subject: [PATCH] Split out SetupFooComponent (#1106) * Split out adding HTTP routes from making internal APIs for clarity * Split out more components * Split out more things * Finish converting * internal mux for internal routes --- appservice/appservice.go | 33 +++++++++-------- clientapi/clientapi.go | 9 ++--- cmd/dendrite-appservice-server/main.go | 6 ++-- cmd/dendrite-client-api-server/main.go | 4 +-- cmd/dendrite-demo-libp2p/main.go | 27 +++++++------- cmd/dendrite-edu-server/main.go | 3 +- cmd/dendrite-federation-api-server/main.go | 4 +-- cmd/dendrite-federation-sender-server/main.go | 3 +- cmd/dendrite-key-server/main.go | 2 +- cmd/dendrite-media-api-server/main.go | 2 +- cmd/dendrite-monolith-server/main.go | 36 ++++++++++--------- cmd/dendrite-public-rooms-api-server/main.go | 2 +- cmd/dendrite-room-server/main.go | 3 +- cmd/dendrite-server-key-api-server/main.go | 3 +- cmd/dendrite-sync-api-server/main.go | 2 +- cmd/dendritejs/main.go | 22 ++++++------ eduserver/eduserver.go | 21 +++++------ federationapi/federationapi.go | 13 +++---- federationsender/federationsender.go | 18 ++++++---- keyserver/keyserver.go | 12 +++---- mediaapi/mediaapi.go | 14 ++++---- publicroomsapi/publicroomsapi.go | 8 +++-- roomserver/roomserver.go | 21 +++++------ serverkeyapi/serverkeyapi.go | 33 ++++++++++------- syncapi/syncapi.go | 8 +++-- 25 files changed, 171 insertions(+), 138 deletions(-) diff --git a/appservice/appservice.go b/appservice/appservice.go index b5ffba5e..dca600bf 100644 --- a/appservice/appservice.go +++ b/appservice/appservice.go @@ -21,6 +21,7 @@ import ( "sync" "time" + "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/appservice/consumers" "github.com/matrix-org/dendrite/appservice/inthttp" @@ -40,15 +41,28 @@ import ( "github.com/sirupsen/logrus" ) -// SetupAppServiceAPIComponent sets up and registers HTTP handlers for the AppServices -// component. -func SetupAppServiceAPIComponent( +// AddPublicRoutes registers HTTP handlers for CS API calls +func AddPublicRoutes(router *mux.Router, cfg *config.Dendrite, rsAPI roomserverAPI.RoomserverInternalAPI, + accountsDB accounts.Database, federation *gomatrixserverlib.FederationClient, txnCache *transactions.Cache) { + + routing.Setup( + router, cfg, rsAPI, + accountsDB, federation, txnCache, + ) +} + +// AddInternalRoutes registers HTTP handlers for internal API calls +func AddInternalRoutes(router *mux.Router, queryAPI appserviceAPI.AppServiceQueryAPI) { + inthttp.AddRoutes(queryAPI, router) +} + +// NewInternalAPI returns a concerete implementation of the internal API. Callers +// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes. +func NewInternalAPI( base *basecomponent.BaseDendrite, accountsDB accounts.Database, deviceDB devices.Database, - federation *gomatrixserverlib.FederationClient, rsAPI roomserverAPI.RoomserverInternalAPI, - transactionsCache *transactions.Cache, ) appserviceAPI.AppServiceQueryAPI { // Create a connection to the appservice postgres DB appserviceDB, err := storage.NewDatabase(string(base.Cfg.Database.AppService), base.Cfg.DbProperties()) @@ -85,8 +99,6 @@ func SetupAppServiceAPIComponent( Cfg: base.Cfg, } - inthttp.AddRoutes(appserviceQueryAPI, base.InternalAPIMux) - consumer := consumers.NewOutputRoomEventConsumer( base.Cfg, base.KafkaConsumer, accountsDB, appserviceDB, rsAPI, workerStates, @@ -99,13 +111,6 @@ func SetupAppServiceAPIComponent( if err := workers.SetupTransactionWorkers(appserviceDB, workerStates); err != nil { logrus.WithError(err).Panicf("failed to start app service transaction workers") } - - // Set up HTTP Endpoints - routing.Setup( - base.PublicAPIMux, base.Cfg, rsAPI, - accountsDB, federation, transactionsCache, - ) - return appserviceQueryAPI } diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index 0ce44c21..d00be6eb 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -15,6 +15,7 @@ package clientapi import ( + "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" @@ -30,9 +31,9 @@ import ( "github.com/sirupsen/logrus" ) -// SetupClientAPIComponent sets up and registers HTTP handlers for the ClientAPI -// component. -func SetupClientAPIComponent( +// AddPublicRoutes sets up and registers HTTP handlers for the ClientAPI component. +func AddPublicRoutes( + router *mux.Router, base *basecomponent.BaseDendrite, deviceDB devices.Database, accountsDB accounts.Database, @@ -65,7 +66,7 @@ func SetupClientAPIComponent( } routing.Setup( - base.PublicAPIMux, base.Cfg, roomserverProducer, rsAPI, asAPI, + router, base.Cfg, roomserverProducer, rsAPI, asAPI, accountsDB, deviceDB, federation, *keyRing, userUpdateProducer, syncProducer, eduProducer, transactionsCache, fsAPI, ) diff --git a/cmd/dendrite-appservice-server/main.go b/cmd/dendrite-appservice-server/main.go index 4fc2a173..21fb1b79 100644 --- a/cmd/dendrite-appservice-server/main.go +++ b/cmd/dendrite-appservice-server/main.go @@ -31,9 +31,9 @@ func main() { rsAPI := base.RoomserverHTTPClient() cache := transactions.New() - appservice.SetupAppServiceAPIComponent( - base, accountDB, deviceDB, federation, rsAPI, cache, - ) + intAPI := appservice.NewInternalAPI(base, accountDB, deviceDB, rsAPI) + appservice.AddInternalRoutes(base.InternalAPIMux, intAPI) + appservice.AddPublicRoutes(base.PublicAPIMux, base.Cfg, rsAPI, accountDB, federation, cache) base.SetupAndServeHTTP(string(base.Cfg.Bind.AppServiceAPI), string(base.Cfg.Listen.AppServiceAPI)) diff --git a/cmd/dendrite-client-api-server/main.go b/cmd/dendrite-client-api-server/main.go index 8ad4231d..58396205 100644 --- a/cmd/dendrite-client-api-server/main.go +++ b/cmd/dendrite-client-api-server/main.go @@ -38,8 +38,8 @@ func main() { fsAPI := base.FederationSenderHTTPClient() eduInputAPI := base.EDUServerClient() - clientapi.SetupClientAPIComponent( - base, deviceDB, accountDB, federation, keyRing, + clientapi.AddPublicRoutes( + base.PublicAPIMux, base, deviceDB, accountDB, federation, keyRing, rsAPI, eduInputAPI, asQuery, transactions.New(), fsAPI, ) diff --git a/cmd/dendrite-demo-libp2p/main.go b/cmd/dendrite-demo-libp2p/main.go index 86909685..80e2e800 100644 --- a/cmd/dendrite-demo-libp2p/main.go +++ b/cmd/dendrite-demo-libp2p/main.go @@ -136,42 +136,41 @@ func main() { deviceDB := base.Base.CreateDeviceDB() federation := createFederationClient(base) - serverKeyAPI := serverkeyapi.SetupServerKeyAPIComponent( - &base.Base, federation, + serverKeyAPI := serverkeyapi.NewInternalAPI( + base.Base.Cfg, federation, base.Base.Caches, ) keyRing := serverKeyAPI.KeyRing() createKeyDB( base, serverKeyAPI, ) - rsAPI := roomserver.SetupRoomServerComponent( + rsAPI := roomserver.NewInternalAPI( &base.Base, keyRing, federation, ) - eduInputAPI := eduserver.SetupEDUServerComponent( + eduInputAPI := eduserver.NewInternalAPI( &base.Base, cache.New(), deviceDB, ) - asAPI := appservice.SetupAppServiceAPIComponent( - &base.Base, accountDB, deviceDB, federation, rsAPI, transactions.New(), - ) - fsAPI := federationsender.SetupFederationSenderComponent( + asAPI := appservice.NewInternalAPI(&base.Base, accountDB, deviceDB, rsAPI) + appservice.AddPublicRoutes(base.Base.PublicAPIMux, &cfg, rsAPI, accountDB, federation, transactions.New()) + fsAPI := federationsender.NewInternalAPI( &base.Base, federation, rsAPI, keyRing, ) rsAPI.SetFederationSenderAPI(fsAPI) - clientapi.SetupClientAPIComponent( - &base.Base, deviceDB, accountDB, + clientapi.AddPublicRoutes( + base.Base.PublicAPIMux, &base.Base, deviceDB, accountDB, federation, keyRing, rsAPI, eduInputAPI, asAPI, transactions.New(), fsAPI, ) eduProducer := producers.NewEDUServerProducer(eduInputAPI) - federationapi.SetupFederationAPIComponent(&base.Base, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer) - mediaapi.SetupMediaAPIComponent(&base.Base, deviceDB) + federationapi.AddPublicRoutes(base.Base.PublicAPIMux, base.Base.Cfg, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer) + mediaapi.AddPublicRoutes(base.Base.PublicAPIMux, base.Base.Cfg, deviceDB) publicRoomsDB, err := storage.NewPublicRoomsServerDatabaseWithPubSub(string(base.Base.Cfg.Database.PublicRoomsAPI), base.LibP2PPubsub, cfg.Matrix.ServerName) if err != nil { logrus.WithError(err).Panicf("failed to connect to public rooms db") } - publicroomsapi.SetupPublicRoomsAPIComponent(&base.Base, deviceDB, publicRoomsDB, rsAPI, federation, nil) // Check this later - syncapi.SetupSyncAPIComponent(&base.Base, deviceDB, accountDB, rsAPI, federation, &cfg) + publicroomsapi.AddPublicRoutes(base.Base.PublicAPIMux, &base.Base, deviceDB, publicRoomsDB, rsAPI, federation, nil) // Check this later + syncapi.AddPublicRoutes(base.Base.PublicAPIMux, &base.Base, deviceDB, accountDB, rsAPI, federation, &cfg) internal.SetupHTTPAPI( http.DefaultServeMux, diff --git a/cmd/dendrite-edu-server/main.go b/cmd/dendrite-edu-server/main.go index ceeb1365..a86f66c8 100644 --- a/cmd/dendrite-edu-server/main.go +++ b/cmd/dendrite-edu-server/main.go @@ -31,7 +31,8 @@ func main() { }() deviceDB := base.CreateDeviceDB() - eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) + intAPI := eduserver.NewInternalAPI(base, cache.New(), deviceDB) + eduserver.AddInternalRoutes(base.InternalAPIMux, intAPI) base.SetupAndServeHTTP(string(base.Cfg.Bind.EDUServer), string(base.Cfg.Listen.EDUServer)) diff --git a/cmd/dendrite-federation-api-server/main.go b/cmd/dendrite-federation-api-server/main.go index 0c4d8b85..7481299e 100644 --- a/cmd/dendrite-federation-api-server/main.go +++ b/cmd/dendrite-federation-api-server/main.go @@ -36,8 +36,8 @@ func main() { // TODO: this isn't a producer eduProducer := producers.NewEDUServerProducer(base.EDUServerClient()) - federationapi.SetupFederationAPIComponent( - base, accountDB, deviceDB, federation, keyRing, + federationapi.AddPublicRoutes( + base.PublicAPIMux, base.Cfg, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer, ) diff --git a/cmd/dendrite-federation-sender-server/main.go b/cmd/dendrite-federation-sender-server/main.go index 651eb257..7b60ad05 100644 --- a/cmd/dendrite-federation-sender-server/main.go +++ b/cmd/dendrite-federation-sender-server/main.go @@ -30,9 +30,10 @@ func main() { keyRing := serverKeyAPI.KeyRing() rsAPI := base.RoomserverHTTPClient() - federationsender.SetupFederationSenderComponent( + fsAPI := federationsender.NewInternalAPI( base, federation, rsAPI, keyRing, ) + federationsender.AddInternalRoutes(base.InternalAPIMux, fsAPI) base.SetupAndServeHTTP(string(base.Cfg.Bind.FederationSender), string(base.Cfg.Listen.FederationSender)) diff --git a/cmd/dendrite-key-server/main.go b/cmd/dendrite-key-server/main.go index a8accbcb..5dccc300 100644 --- a/cmd/dendrite-key-server/main.go +++ b/cmd/dendrite-key-server/main.go @@ -27,7 +27,7 @@ func main() { accountDB := base.CreateAccountsDB() deviceDB := base.CreateDeviceDB() - keyserver.SetupKeyServerComponent(base, deviceDB, accountDB) + keyserver.AddPublicRoutes(base.PublicAPIMux, base.Cfg, deviceDB, accountDB) base.SetupAndServeHTTP(string(base.Cfg.Bind.KeyServer), string(base.Cfg.Listen.KeyServer)) diff --git a/cmd/dendrite-media-api-server/main.go b/cmd/dendrite-media-api-server/main.go index 01855938..5c65fad0 100644 --- a/cmd/dendrite-media-api-server/main.go +++ b/cmd/dendrite-media-api-server/main.go @@ -26,7 +26,7 @@ func main() { deviceDB := base.CreateDeviceDB() - mediaapi.SetupMediaAPIComponent(base, deviceDB) + mediaapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, deviceDB) base.SetupAndServeHTTP(string(base.Cfg.Bind.MediaAPI), string(base.Cfg.Listen.MediaAPI)) diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 57e8f66f..97fa3a2a 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -70,62 +70,66 @@ func main() { deviceDB := base.CreateDeviceDB() federation := base.CreateFederationClient() - serverKeyAPI := serverkeyapi.SetupServerKeyAPIComponent( - base, federation, + serverKeyAPI := serverkeyapi.NewInternalAPI( + base.Cfg, federation, base.Caches, ) if base.UseHTTPAPIs { + serverkeyapi.AddInternalRoutes(base.InternalAPIMux, serverKeyAPI, base.Caches) serverKeyAPI = base.ServerKeyAPIClient() } keyRing := serverKeyAPI.KeyRing() - rsComponent := roomserver.SetupRoomServerComponent( + rsComponent := roomserver.NewInternalAPI( base, keyRing, federation, ) rsAPI := rsComponent if base.UseHTTPAPIs { + roomserver.AddInternalRoutes(base.InternalAPIMux, rsAPI) rsAPI = base.RoomserverHTTPClient() } - eduInputAPI := eduserver.SetupEDUServerComponent( + eduInputAPI := eduserver.NewInternalAPI( base, cache.New(), deviceDB, ) if base.UseHTTPAPIs { + eduserver.AddInternalRoutes(base.InternalAPIMux, eduInputAPI) eduInputAPI = base.EDUServerClient() } - asAPI := appservice.SetupAppServiceAPIComponent( - base, accountDB, deviceDB, federation, rsAPI, transactions.New(), - ) + asAPI := appservice.NewInternalAPI(base, accountDB, deviceDB, rsAPI) + appservice.AddPublicRoutes(base.PublicAPIMux, cfg, rsAPI, accountDB, federation, transactions.New()) if base.UseHTTPAPIs { + appservice.AddInternalRoutes(base.InternalAPIMux, asAPI) asAPI = base.AppserviceHTTPClient() } - fsAPI := federationsender.SetupFederationSenderComponent( + fsAPI := federationsender.NewInternalAPI( base, federation, rsAPI, keyRing, ) if base.UseHTTPAPIs { + federationsender.AddInternalRoutes(base.InternalAPIMux, fsAPI) fsAPI = base.FederationSenderHTTPClient() } rsComponent.SetFederationSenderAPI(fsAPI) - clientapi.SetupClientAPIComponent( - base, deviceDB, accountDB, + clientapi.AddPublicRoutes( + base.PublicAPIMux, base, deviceDB, accountDB, federation, keyRing, rsAPI, eduInputAPI, asAPI, transactions.New(), fsAPI, ) - keyserver.SetupKeyServerComponent( - base, deviceDB, accountDB, + keyserver.AddPublicRoutes( + base.PublicAPIMux, base.Cfg, deviceDB, accountDB, ) eduProducer := producers.NewEDUServerProducer(eduInputAPI) - federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer) - mediaapi.SetupMediaAPIComponent(base, deviceDB) + federationapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer) + mediaapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, deviceDB) publicRoomsDB, err := storage.NewPublicRoomsServerDatabase(string(base.Cfg.Database.PublicRoomsAPI), base.Cfg.DbProperties(), cfg.Matrix.ServerName) if err != nil { logrus.WithError(err).Panicf("failed to connect to public rooms db") } - publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB, publicRoomsDB, rsAPI, federation, nil) - syncapi.SetupSyncAPIComponent(base, deviceDB, accountDB, rsAPI, federation, cfg) + publicroomsapi.AddPublicRoutes(base.PublicAPIMux, base, deviceDB, publicRoomsDB, rsAPI, federation, nil) + syncapi.AddPublicRoutes(base.PublicAPIMux, base, deviceDB, accountDB, rsAPI, federation, cfg) internal.SetupHTTPAPI( http.DefaultServeMux, diff --git a/cmd/dendrite-public-rooms-api-server/main.go b/cmd/dendrite-public-rooms-api-server/main.go index 0cad3670..413d7ecb 100644 --- a/cmd/dendrite-public-rooms-api-server/main.go +++ b/cmd/dendrite-public-rooms-api-server/main.go @@ -34,7 +34,7 @@ func main() { if err != nil { logrus.WithError(err).Panicf("failed to connect to public rooms db") } - publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB, publicRoomsDB, rsAPI, nil, nil) + publicroomsapi.AddPublicRoutes(base.PublicAPIMux, base, deviceDB, publicRoomsDB, rsAPI, nil, nil) base.SetupAndServeHTTP(string(base.Cfg.Bind.PublicRoomsAPI), string(base.Cfg.Listen.PublicRoomsAPI)) diff --git a/cmd/dendrite-room-server/main.go b/cmd/dendrite-room-server/main.go index 79745cde..a2f1e941 100644 --- a/cmd/dendrite-room-server/main.go +++ b/cmd/dendrite-room-server/main.go @@ -29,8 +29,9 @@ func main() { keyRing := serverKeyAPI.KeyRing() fsAPI := base.FederationSenderHTTPClient() - rsAPI := roomserver.SetupRoomServerComponent(base, keyRing, federation) + rsAPI := roomserver.NewInternalAPI(base, keyRing, federation) rsAPI.SetFederationSenderAPI(fsAPI) + roomserver.AddInternalRoutes(base.InternalAPIMux, rsAPI) base.SetupAndServeHTTP(string(base.Cfg.Bind.RoomServer), string(base.Cfg.Listen.RoomServer)) diff --git a/cmd/dendrite-server-key-api-server/main.go b/cmd/dendrite-server-key-api-server/main.go index e206978c..b4bfcbff 100644 --- a/cmd/dendrite-server-key-api-server/main.go +++ b/cmd/dendrite-server-key-api-server/main.go @@ -26,7 +26,8 @@ func main() { federation := base.CreateFederationClient() - serverkeyapi.SetupServerKeyAPIComponent(base, federation) + intAPI := serverkeyapi.NewInternalAPI(base.Cfg, federation, base.Caches) + serverkeyapi.AddInternalRoutes(base.InternalAPIMux, intAPI, base.Caches) base.SetupAndServeHTTP(string(base.Cfg.Bind.ServerKeyAPI), string(base.Cfg.Listen.ServerKeyAPI)) } diff --git a/cmd/dendrite-sync-api-server/main.go b/cmd/dendrite-sync-api-server/main.go index bb56b4ab..4ad68c5e 100644 --- a/cmd/dendrite-sync-api-server/main.go +++ b/cmd/dendrite-sync-api-server/main.go @@ -30,7 +30,7 @@ func main() { rsAPI := base.RoomserverHTTPClient() - syncapi.SetupSyncAPIComponent(base, deviceDB, accountDB, rsAPI, federation, cfg) + syncapi.AddPublicRoutes(base.PublicAPIMux, base, deviceDB, accountDB, rsAPI, federation, cfg) base.SetupAndServeHTTP(string(base.Cfg.Bind.SyncAPI), string(base.Cfg.Listen.SyncAPI)) diff --git a/cmd/dendritejs/main.go b/cmd/dendritejs/main.go index 4c1b8197..9c6c7c03 100644 --- a/cmd/dendritejs/main.go +++ b/cmd/dendritejs/main.go @@ -206,29 +206,29 @@ func main() { KeyDatabase: fetcher, } - rsAPI := roomserver.SetupRoomServerComponent(base, keyRing, federation) - eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) - asQuery := appservice.SetupAppServiceAPIComponent( - base, accountDB, deviceDB, federation, rsAPI, transactions.New(), + rsAPI := roomserver.NewInternalAPI(base, keyRing, federation) + eduInputAPI := eduserver.NewInternalAPI(base, cache.New(), deviceDB) + asQuery := appservice.NewInternalAPI( + base, accountDB, deviceDB, rsAPI, ) - fedSenderAPI := federationsender.SetupFederationSenderComponent(base, federation, rsAPI, &keyRing) + fedSenderAPI := federationsender.NewInternalAPI(base, federation, rsAPI, &keyRing) rsAPI.SetFederationSenderAPI(fedSenderAPI) p2pPublicRoomProvider := NewLibP2PPublicRoomsProvider(node, fedSenderAPI) - clientapi.SetupClientAPIComponent( - base, deviceDB, accountDB, + clientapi.AddPublicRoutes( + base.PublicAPIMux, base, deviceDB, accountDB, federation, &keyRing, rsAPI, eduInputAPI, asQuery, transactions.New(), fedSenderAPI, ) eduProducer := producers.NewEDUServerProducer(eduInputAPI) - federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, &keyRing, rsAPI, asQuery, fedSenderAPI, eduProducer) - mediaapi.SetupMediaAPIComponent(base, deviceDB) + federationapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, accountDB, deviceDB, federation, &keyRing, rsAPI, asQuery, fedSenderAPI, eduProducer) + mediaapi.AddPublicRoutes(base.PublicAPIMux, base.Cfg, deviceDB) publicRoomsDB, err := storage.NewPublicRoomsServerDatabase(string(base.Cfg.Database.PublicRoomsAPI), cfg.Matrix.ServerName) if err != nil { logrus.WithError(err).Panicf("failed to connect to public rooms db") } - publicroomsapi.SetupPublicRoomsAPIComponent(base, deviceDB, publicRoomsDB, rsAPI, federation, p2pPublicRoomProvider) - syncapi.SetupSyncAPIComponent(base, deviceDB, accountDB, rsAPI, federation, cfg) + publicroomsapi.AddPublicRoutes(base.PublicAPIMux, base, deviceDB, publicRoomsDB, rsAPI, federation, p2pPublicRoomProvider) + syncapi.AddPublicRoutes(base.PublicAPIMux, base, deviceDB, accountDB, rsAPI, federation, cfg) internal.SetupHTTPAPI( http.DefaultServeMux, diff --git a/eduserver/eduserver.go b/eduserver/eduserver.go index 59decc8f..c14be3f6 100644 --- a/eduserver/eduserver.go +++ b/eduserver/eduserver.go @@ -17,6 +17,7 @@ package eduserver import ( + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/eduserver/api" "github.com/matrix-org/dendrite/eduserver/cache" @@ -25,16 +26,20 @@ import ( "github.com/matrix-org/dendrite/internal/basecomponent" ) -// SetupEDUServerComponent sets up and registers HTTP handlers for the -// EDUServer component. Returns instances of the various roomserver APIs, -// allowing other components running in the same process to hit the query the -// APIs directly instead of having to use HTTP. -func SetupEDUServerComponent( +// AddInternalRoutes registers HTTP handlers for the internal API. Invokes functions +// on the given input API. +func AddInternalRoutes(internalMux *mux.Router, inputAPI api.EDUServerInputAPI) { + inthttp.AddRoutes(inputAPI, internalMux) +} + +// NewInternalAPI returns a concerete implementation of the internal API. Callers +// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes. +func NewInternalAPI( base *basecomponent.BaseDendrite, eduCache *cache.EDUCache, deviceDB devices.Database, ) api.EDUServerInputAPI { - inputAPI := &input.EDUServerInputAPI{ + return &input.EDUServerInputAPI{ Cache: eduCache, DeviceDB: deviceDB, Producer: base.KafkaProducer, @@ -42,8 +47,4 @@ func SetupEDUServerComponent( OutputSendToDeviceEventTopic: string(base.Cfg.Kafka.Topics.OutputSendToDeviceEvent), ServerName: base.Cfg.Matrix.ServerName, } - - inthttp.AddRoutes(inputAPI, base.InternalAPIMux) - - return inputAPI } diff --git a/federationapi/federationapi.go b/federationapi/federationapi.go index baeaa36b..7aecd272 100644 --- a/federationapi/federationapi.go +++ b/federationapi/federationapi.go @@ -15,11 +15,12 @@ package federationapi import ( + "github.com/gorilla/mux" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" - "github.com/matrix-org/dendrite/internal/basecomponent" + "github.com/matrix-org/dendrite/internal/config" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" // TODO: Are we really wanting to pull in the producer from clientapi @@ -28,10 +29,10 @@ import ( "github.com/matrix-org/gomatrixserverlib" ) -// SetupFederationAPIComponent sets up and registers HTTP handlers for the -// FederationAPI component. -func SetupFederationAPIComponent( - base *basecomponent.BaseDendrite, +// AddPublicRoutes sets up and registers HTTP handlers on the base API muxes for the FederationAPI component. +func AddPublicRoutes( + router *mux.Router, + cfg *config.Dendrite, accountsDB accounts.Database, deviceDB devices.Database, federation *gomatrixserverlib.FederationClient, @@ -44,7 +45,7 @@ func SetupFederationAPIComponent( roomserverProducer := producers.NewRoomserverProducer(rsAPI) routing.Setup( - base.PublicAPIMux, base.Cfg, rsAPI, asAPI, roomserverProducer, + router, cfg, rsAPI, asAPI, roomserverProducer, eduProducer, federationSenderAPI, *keyRing, federation, accountsDB, deviceDB, ) diff --git a/federationsender/federationsender.go b/federationsender/federationsender.go index e25c2723..e3e0ef9d 100644 --- a/federationsender/federationsender.go +++ b/federationsender/federationsender.go @@ -15,6 +15,7 @@ package federationsender import ( + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/federationsender/consumers" "github.com/matrix-org/dendrite/federationsender/internal" @@ -29,9 +30,15 @@ import ( "github.com/sirupsen/logrus" ) -// SetupFederationSenderComponent sets up and registers HTTP handlers for the -// FederationSender component. -func SetupFederationSenderComponent( +// AddInternalRoutes registers HTTP handlers for the internal API. Invokes functions +// on the given input API. +func AddInternalRoutes(router *mux.Router, intAPI api.FederationSenderInternalAPI) { + inthttp.AddRoutes(intAPI, router) +} + +// NewInternalAPI returns a concerete implementation of the internal API. Callers +// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes. +func NewInternalAPI( base *basecomponent.BaseDendrite, federation *gomatrixserverlib.FederationClient, rsAPI roomserverAPI.RoomserverInternalAPI, @@ -66,8 +73,5 @@ func SetupFederationSenderComponent( logrus.WithError(err).Panic("failed to start typing server consumer") } - queryAPI := internal.NewFederationSenderInternalAPI(federationSenderDB, base.Cfg, roomserverProducer, federation, keyRing, statistics, queues) - inthttp.AddRoutes(queryAPI, base.InternalAPIMux) - - return queryAPI + return internal.NewFederationSenderInternalAPI(federationSenderDB, base.Cfg, roomserverProducer, federation, keyRing, statistics, queues) } diff --git a/keyserver/keyserver.go b/keyserver/keyserver.go index 4e785cbc..1eb73054 100644 --- a/keyserver/keyserver.go +++ b/keyserver/keyserver.go @@ -15,18 +15,18 @@ package keyserver import ( + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" - "github.com/matrix-org/dendrite/internal/basecomponent" + "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/keyserver/routing" ) -// SetupFederationSenderComponent sets up and registers HTTP handlers for the -// FederationSender component. -func SetupKeyServerComponent( - base *basecomponent.BaseDendrite, +// AddPublicRoutes registers HTTP handlers for CS API calls +func AddPublicRoutes( + router *mux.Router, cfg *config.Dendrite, deviceDB devices.Database, accountsDB accounts.Database, ) { - routing.Setup(base.PublicAPIMux, base.Cfg, accountsDB, deviceDB) + routing.Setup(router, cfg, accountsDB, deviceDB) } diff --git a/mediaapi/mediaapi.go b/mediaapi/mediaapi.go index b5bec390..d4e260ea 100644 --- a/mediaapi/mediaapi.go +++ b/mediaapi/mediaapi.go @@ -15,26 +15,26 @@ package mediaapi import ( + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" - "github.com/matrix-org/dendrite/internal/basecomponent" + "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/mediaapi/routing" "github.com/matrix-org/dendrite/mediaapi/storage" "github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus" ) -// SetupMediaAPIComponent sets up and registers HTTP handlers for the MediaAPI -// component. -func SetupMediaAPIComponent( - base *basecomponent.BaseDendrite, +// AddPublicRoutes sets up and registers HTTP handlers for the MediaAPI component. +func AddPublicRoutes( + router *mux.Router, cfg *config.Dendrite, deviceDB devices.Database, ) { - mediaDB, err := storage.Open(string(base.Cfg.Database.MediaAPI), base.Cfg.DbProperties()) + mediaDB, err := storage.Open(string(cfg.Database.MediaAPI), cfg.DbProperties()) if err != nil { logrus.WithError(err).Panicf("failed to connect to media db") } routing.Setup( - base.PublicAPIMux, base.Cfg, mediaDB, deviceDB, gomatrixserverlib.NewClient(), + router, cfg, mediaDB, deviceDB, gomatrixserverlib.NewClient(), ) } diff --git a/publicroomsapi/publicroomsapi.go b/publicroomsapi/publicroomsapi.go index b53351ff..280ab9e3 100644 --- a/publicroomsapi/publicroomsapi.go +++ b/publicroomsapi/publicroomsapi.go @@ -15,6 +15,7 @@ package publicroomsapi import ( + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth/storage/devices" "github.com/matrix-org/dendrite/internal/basecomponent" "github.com/matrix-org/dendrite/publicroomsapi/consumers" @@ -26,9 +27,10 @@ import ( "github.com/sirupsen/logrus" ) -// SetupPublicRoomsAPIComponent sets up and registers HTTP handlers for the PublicRoomsAPI +// AddPublicRoutes sets up and registers HTTP handlers for the PublicRoomsAPI // component. -func SetupPublicRoomsAPIComponent( +func AddPublicRoutes( + router *mux.Router, base *basecomponent.BaseDendrite, deviceDB devices.Database, publicRoomsDB storage.Database, @@ -43,5 +45,5 @@ func SetupPublicRoomsAPIComponent( logrus.WithError(err).Panic("failed to start public rooms server consumer") } - routing.Setup(base.PublicAPIMux, deviceDB, publicRoomsDB, rsAPI, fedClient, extRoomsProvider) + routing.Setup(router, deviceDB, publicRoomsDB, rsAPI, fedClient, extRoomsProvider) } diff --git a/roomserver/roomserver.go b/roomserver/roomserver.go index a55b20be..a9db22d7 100644 --- a/roomserver/roomserver.go +++ b/roomserver/roomserver.go @@ -15,6 +15,7 @@ package roomserver import ( + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/roomserver/inthttp" "github.com/matrix-org/gomatrixserverlib" @@ -25,11 +26,15 @@ import ( "github.com/sirupsen/logrus" ) -// SetupRoomServerComponent sets up and registers HTTP handlers for the -// RoomServer component. Returns instances of the various roomserver APIs, -// allowing other components running in the same process to hit the query the -// APIs directly instead of having to use HTTP. -func SetupRoomServerComponent( +// AddInternalRoutes registers HTTP handlers for the internal API. Invokes functions +// on the given input API. +func AddInternalRoutes(router *mux.Router, intAPI api.RoomserverInternalAPI) { + inthttp.AddRoutes(intAPI, router) +} + +// NewInternalAPI returns a concerete implementation of the internal API. Callers +// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes. +func NewInternalAPI( base *basecomponent.BaseDendrite, keyRing gomatrixserverlib.JSONVerifier, fedClient *gomatrixserverlib.FederationClient, @@ -39,7 +44,7 @@ func SetupRoomServerComponent( logrus.WithError(err).Panicf("failed to connect to room server db") } - internalAPI := &internal.RoomserverInternalAPI{ + return &internal.RoomserverInternalAPI{ DB: roomserverDB, Cfg: base.Cfg, Producer: base.KafkaProducer, @@ -49,8 +54,4 @@ func SetupRoomServerComponent( FedClient: fedClient, KeyRing: keyRing, } - - inthttp.AddRoutes(internalAPI, base.InternalAPIMux) - - return internalAPI } diff --git a/serverkeyapi/serverkeyapi.go b/serverkeyapi/serverkeyapi.go index ad885270..58ca00b7 100644 --- a/serverkeyapi/serverkeyapi.go +++ b/serverkeyapi/serverkeyapi.go @@ -4,7 +4,9 @@ import ( "crypto/ed25519" "encoding/base64" - "github.com/matrix-org/dendrite/internal/basecomponent" + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/internal/caching" + "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/matrix-org/dendrite/serverkeyapi/internal" "github.com/matrix-org/dendrite/serverkeyapi/inthttp" @@ -14,22 +16,31 @@ import ( "github.com/sirupsen/logrus" ) -func SetupServerKeyAPIComponent( - base *basecomponent.BaseDendrite, +// AddInternalRoutes registers HTTP handlers for the internal API. Invokes functions +// on the given input API. +func AddInternalRoutes(router *mux.Router, intAPI api.ServerKeyInternalAPI, caches *caching.Caches) { + inthttp.AddRoutes(intAPI, router, caches) +} + +// NewInternalAPI returns a concerete implementation of the internal API. Callers +// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes. +func NewInternalAPI( + cfg *config.Dendrite, fedClient *gomatrixserverlib.FederationClient, + caches *caching.Caches, ) api.ServerKeyInternalAPI { innerDB, err := storage.NewDatabase( - string(base.Cfg.Database.ServerKey), - base.Cfg.DbProperties(), - base.Cfg.Matrix.ServerName, - base.Cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey), - base.Cfg.Matrix.KeyID, + string(cfg.Database.ServerKey), + cfg.DbProperties(), + cfg.Matrix.ServerName, + cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey), + cfg.Matrix.KeyID, ) if err != nil { logrus.WithError(err).Panicf("failed to connect to server key database") } - serverKeyDB, err := cache.NewKeyDatabase(innerDB, base.Caches) + serverKeyDB, err := cache.NewKeyDatabase(innerDB, caches) if err != nil { logrus.WithError(err).Panicf("failed to set up caching wrapper for server key database") } @@ -47,7 +58,7 @@ func SetupServerKeyAPIComponent( } var b64e = base64.StdEncoding.WithPadding(base64.NoPadding) - for _, ps := range base.Cfg.Matrix.KeyPerspectives { + for _, ps := range cfg.Matrix.KeyPerspectives { perspective := &gomatrixserverlib.PerspectiveKeyFetcher{ PerspectiveServerName: ps.ServerName, PerspectiveServerKeys: map[gomatrixserverlib.KeyID]ed25519.PublicKey{}, @@ -77,7 +88,5 @@ func SetupServerKeyAPIComponent( }).Info("Enabled perspective key fetcher") } - inthttp.AddRoutes(&internalAPI, base.InternalAPIMux, base.Caches) - return &internalAPI } diff --git a/syncapi/syncapi.go b/syncapi/syncapi.go index 762f4e9d..6e84dcb6 100644 --- a/syncapi/syncapi.go +++ b/syncapi/syncapi.go @@ -17,6 +17,7 @@ package syncapi import ( "context" + "github.com/gorilla/mux" "github.com/sirupsen/logrus" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" @@ -32,9 +33,10 @@ import ( "github.com/matrix-org/dendrite/syncapi/sync" ) -// SetupSyncAPIComponent sets up and registers HTTP handlers for the SyncAPI +// AddPublicRoutes sets up and registers HTTP handlers for the SyncAPI // component. -func SetupSyncAPIComponent( +func AddPublicRoutes( + router *mux.Router, base *basecomponent.BaseDendrite, deviceDB devices.Database, accountsDB accounts.Database, @@ -88,5 +90,5 @@ func SetupSyncAPIComponent( logrus.WithError(err).Panicf("failed to start send-to-device consumer") } - routing.Setup(base.PublicAPIMux, requestPool, syncDB, deviceDB, federation, rsAPI, cfg) + routing.Setup(router, requestPool, syncDB, deviceDB, federation, rsAPI, cfg) }