diff --git a/api/account/account.go b/api/account/account.go index f88d624..9893599 100644 --- a/api/account/account.go +++ b/api/account/account.go @@ -4,6 +4,8 @@ import ( "context" "crypto/ed25519" + "github.com/julienschmidt/httprouter" + "git.lumeweb.com/LumeWeb/portal/account" "git.lumeweb.com/LumeWeb/portal/api/middleware" "git.lumeweb.com/LumeWeb/portal/api/registry" @@ -45,10 +47,6 @@ func NewS5(params AccountAPIParams) AccountApiResult { } } -func InitAPI(api *AccountAPI) error { - return api.Init() -} - var Module = fx.Module("s5_api", fx.Provide(NewS5), fx.Provide(NewHttpHandler), @@ -65,7 +63,6 @@ func (a AccountAPI) Name() string { } func (a *AccountAPI) Init() error { - middleware.RegisterProtocolSubdomain(a.config, jape.Mux(getRoutes(a)), "s5") return nil } @@ -77,8 +74,7 @@ func (a AccountAPI) Stop(ctx context.Context) error { return nil } -func getRoutes(a *AccountAPI) map[string]jape.Handler { - +func (a AccountAPI) Routes() *httprouter.Router { authMw2fa := authMiddleware(middleware.AuthMiddlewareOptions{ Identity: a.identity, Accounts: a.accounts, @@ -93,12 +89,12 @@ func getRoutes(a *AccountAPI) map[string]jape.Handler { Purpose: account.JWTPurposeLogin, }) - return map[string]jape.Handler{ + return jape.Mux(map[string]jape.Handler{ "/api/auth/login": middleware.ApplyMiddlewares(a.httpHandler.login, authMw2fa, middleware.ProxyMiddleware), "/api/auth/register": a.httpHandler.register, "/api/auth/otp/generate": middleware.ApplyMiddlewares(a.httpHandler.otpGenerate, authMw, middleware.ProxyMiddleware), "/api/auth/otp/verify": middleware.ApplyMiddlewares(a.httpHandler.otpVerify, authMw, middleware.ProxyMiddleware), "/api/auth/otp/validate": middleware.ApplyMiddlewares(a.httpHandler.otpValidate, authMw, middleware.ProxyMiddleware), "/api/auth/otp/disable": middleware.ApplyMiddlewares(a.httpHandler.otpDisable, authMw, middleware.ProxyMiddleware), - } + }) } diff --git a/api/api.go b/api/api.go index 928d01d..eb6af0e 100644 --- a/api/api.go +++ b/api/api.go @@ -2,24 +2,25 @@ package api import ( "context" + "slices" + + "git.lumeweb.com/LumeWeb/portal/api/middleware" + "git.lumeweb.com/LumeWeb/portal/api/account" "git.lumeweb.com/LumeWeb/portal/api/registry" "git.lumeweb.com/LumeWeb/portal/api/s5" - "github.com/samber/lo" "github.com/spf13/viper" "go.uber.org/fx" ) func RegisterApis() { registry.Register(registry.APIEntry{ - Key: "s5", - Module: s5.Module, - InitFunc: s5.InitAPI, + Key: "s5", + Module: s5.Module, }) registry.Register(registry.APIEntry{ - Key: "account", - Module: account.Module, - InitFunc: account.InitAPI, + Key: "account", + Module: account.Module, }) } @@ -27,14 +28,28 @@ func BuildApis(config *viper.Viper) fx.Option { var options []fx.Option enabledProtocols := config.GetStringSlice("core.protocols") for _, entry := range registry.GetRegistry() { - if lo.Contains(enabledProtocols, entry.Key) { + if slices.Contains(enabledProtocols, entry.Key) { options = append(options, entry.Module) - if entry.InitFunc != nil { - options = append(options, fx.Invoke(entry.InitFunc)) - } } } + options = append(options, fx.Invoke(func(protocols []registry.API) error { + for _, protocol := range protocols { + err := protocol.Init() + if err != nil { + return err + } + } + + return nil + })) + + options = append(options, fx.Invoke(func(protocols []registry.API) { + for _, protocol := range protocols { + middleware.RegisterProtocolSubdomain(config, protocol.Routes(), protocol.Name()) + } + })) + return fx.Module("api", fx.Options(options...)) } diff --git a/api/registry/registry.go b/api/registry/registry.go index 2c4593c..951b7e0 100644 --- a/api/registry/registry.go +++ b/api/registry/registry.go @@ -2,6 +2,9 @@ package registry import ( "context" + + "github.com/julienschmidt/httprouter" + router2 "git.lumeweb.com/LumeWeb/portal/api/router" "go.uber.org/fx" ) @@ -11,12 +14,12 @@ type API interface { Init() error Start(ctx context.Context) error Stop(ctx context.Context) error + Routes() *httprouter.Router } type APIEntry struct { - Key string - Module fx.Option - InitFunc interface{} + Key string + Module fx.Option } var apiRegistry []APIEntry diff --git a/api/s5/s5.go b/api/s5/s5.go index 1a4bf87..78dd0dd 100644 --- a/api/s5/s5.go +++ b/api/s5/s5.go @@ -6,6 +6,12 @@ import ( "embed" _ "embed" "fmt" + "io/fs" + "net/http" + "net/url" + + "github.com/julienschmidt/httprouter" + "git.lumeweb.com/LumeWeb/portal/account" "git.lumeweb.com/LumeWeb/portal/api/middleware" "git.lumeweb.com/LumeWeb/portal/api/registry" @@ -17,9 +23,6 @@ import ( "github.com/spf13/viper" "go.sia.tech/jape" "go.uber.org/fx" - "io/fs" - "net/http" - "net/url" ) var ( @@ -75,10 +78,6 @@ func NewS5(params APIParams) (S5ApiResult, error) { }, nil } -func InitAPI(api *S5API) error { - return api.Init() -} - var Module = fx.Module("s5_api", fx.Provide(NewS5), fx.Provide(NewHttpHandler), @@ -92,8 +91,6 @@ func (s *S5API) Init() error { s5protocolInstance := s5protocol.(*s5.S5Protocol) s.protocol = s5protocolInstance - router := s5protocolInstance.Node().Services().HTTP().GetHttpRouter(getRoutes(s)) - middleware.RegisterProtocolSubdomain(s.config, router, "s5") return nil } @@ -110,14 +107,7 @@ func (s S5API) Stop(ctx context.Context) error { return nil } -func byteHandler(b []byte) jape.Handler { - return func(c jape.Context) { - c.ResponseWriter.Header().Set("Content-Type", "application/json") - c.ResponseWriter.Write(b) - } -} - -func getRoutes(s *S5API) map[string]jape.Handler { +func (s *S5API) Routes() *httprouter.Router { authMiddlewareOpts := middleware.AuthMiddlewareOptions{ Identity: s.identity, Accounts: s.accounts, @@ -167,7 +157,7 @@ func getRoutes(s *S5API) map[string]jape.Handler { http.Redirect(jc.ResponseWriter, jc.Request, "/swagger/", http.StatusMovedPermanently) } - return map[string]jape.Handler{ + routes := map[string]jape.Handler{ // Account API "GET /s5/account/register": s.httpHandler.accountRegisterChallenge, "POST /s5/account/register": s.httpHandler.accountRegister, @@ -211,6 +201,15 @@ func getRoutes(s *S5API) map[string]jape.Handler { "GET /swagger": swaggerRedirect, "GET /swagger/*path": middleware.ApplyMiddlewares(swaggerHandler, swaggerStrip), } + + return s.protocol.Node().Services().HTTP().GetHttpRouter(routes) +} + +func byteHandler(b []byte) jape.Handler { + return func(c jape.Context) { + c.ResponseWriter.Header().Set("Content-Type", "application/json") + c.ResponseWriter.Write(b) + } } type s5TusJwtResponseWriter struct {