diff --git a/cmd/dendrite-demo-libp2p/main.go b/cmd/dendrite-demo-libp2p/main.go index d1e16db70..80e2e800f 100644 --- a/cmd/dendrite-demo-libp2p/main.go +++ b/cmd/dendrite-demo-libp2p/main.go @@ -136,15 +136,15 @@ 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.NewInternalAPI( @@ -169,8 +169,8 @@ func main() { 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-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 711ebaadf..97fa3a2a6 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -70,19 +70,21 @@ 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() } @@ -126,8 +128,8 @@ func main() { 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..ec7f2654c 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.PublicAPIMux, 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 58b33e40f..9c6c7c031 100644 --- a/cmd/dendritejs/main.go +++ b/cmd/dendritejs/main.go @@ -206,7 +206,7 @@ func main() { KeyDatabase: fetcher, } - rsAPI := roomserver.SetupRoomServerComponent(base, keyRing, federation) + rsAPI := roomserver.NewInternalAPI(base, keyRing, federation) eduInputAPI := eduserver.NewInternalAPI(base, cache.New(), deviceDB) asQuery := appservice.NewInternalAPI( base, accountDB, deviceDB, rsAPI, @@ -227,8 +227,8 @@ func main() { 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/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) }