refactor: change how init of protocols works and make router building part of the interface

This commit is contained in:
Derrick Hammer 2024-02-16 08:39:55 -05:00
parent 1b3934c793
commit f0d7a337db
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 54 additions and 41 deletions

View File

@ -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),
}
})
}

View File

@ -2,10 +2,13 @@ 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"
)
@ -14,12 +17,10 @@ func RegisterApis() {
registry.Register(registry.APIEntry{
Key: "s5",
Module: s5.Module,
InitFunc: s5.InitAPI,
})
registry.Register(registry.APIEntry{
Key: "account",
Module: account.Module,
InitFunc: account.InitAPI,
})
}
@ -27,13 +28,27 @@ 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...))
}

View File

@ -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{}
}
var apiRegistry []APIEntry

View File

@ -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 {