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 b5ffba5eb..dca600bf5 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 0ce44c211..d00be6eb0 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 4fc2a173d..21fb1b79c 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 8ad4231d1..58396205c 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 86909685a..80e2e800f 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 ceeb13658..a86f66c89 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 0c4d8b854..7481299e9 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 651eb257f..7b60ad055 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 a8accbcb3..5dccc3004 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 018559383..5c65fad05 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 57e8f66f5..97fa3a2a6 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 0cad3670c..413d7ecbb 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 79745cded..a2f1e9415 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 e206978cd..b4bfcbffb 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 bb56b4ab0..4ad68c5e8 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 4c1b81979..9c6c7c031 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 59decc8fb..c14be3f67 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 baeaa36bc..7aecd272b 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 e25c27237..e3e0ef9d8 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 4e785cbc7..1eb730541 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 b5bec3907..d4e260ea4 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 b53351ffd..280ab9e3d 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 a55b20bef..a9db22d7a 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 ad885270a..58ca00b73 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 762f4e9d3..6e84dcb6f 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) }