From a1005ce6bfe3c5231b1c62f7d8147d9ff435a623 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Tue, 20 Dec 2022 16:26:04 -0500 Subject: [PATCH] works --- pkg/auth/authenticator.go | 6 ++--- pkg/blas/blas.go | 51 +++++++++++++++--------------------- pkg/blas/components.go | 17 +++++------- pkg/blas/core/core.go | 29 ++++++++++++++++++++ pkg/cmd/serve/cmd.go | 2 +- pkg/components/components.go | 17 ++++++++++++ pkg/frontend/frontend.go | 5 +++- pkg/server/server.go | 6 +++-- pkg/wsapi/api.go | 2 +- 9 files changed, 86 insertions(+), 49 deletions(-) create mode 100644 pkg/blas/core/core.go create mode 100644 pkg/components/components.go diff --git a/pkg/auth/authenticator.go b/pkg/auth/authenticator.go index a4893b3..ed4c236 100644 --- a/pkg/auth/authenticator.go +++ b/pkg/auth/authenticator.go @@ -9,6 +9,7 @@ import ( "github.com/rs/zerolog/log" "dynatron.me/x/blasphem/pkg/auth/provider" + "dynatron.me/x/blasphem/pkg/components" "dynatron.me/x/blasphem/pkg/storage" // providers @@ -34,6 +35,7 @@ type authenticator struct { type Authenticator interface { ValidateAccessToken(token AccessToken) *RefreshToken + InstallRoutes(e *echo.Echo, comp components.Componenter) } type AuthError struct { @@ -41,10 +43,8 @@ type AuthError struct { Description string `json:"error_description"` } -func (a *authenticator) InstallRoutes(e *echo.Echo) { +func (a *authenticator) InstallRoutes(e *echo.Echo, comp components.Componenter) { authG := e.Group("/auth") - panic("reinstall authorize") - //authG.GET("/authorize", frontend.AliasHandler("authorize.html")) authG.GET("/providers", a.ProvidersHandler) authG.POST("/token", a.TokenHandler) diff --git a/pkg/blas/blas.go b/pkg/blas/blas.go index 12555f5..9fdc490 100644 --- a/pkg/blas/blas.go +++ b/pkg/blas/blas.go @@ -9,43 +9,22 @@ import ( "dynatron.me/x/blasphem/internal/common" "dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/bus" + "dynatron.me/x/blasphem/pkg/components" "dynatron.me/x/blasphem/pkg/config" "dynatron.me/x/blasphem/pkg/storage" + + "github.com/rs/zerolog/log" ) -type Core interface { - auth.Authenticator - bus.Bus - storage.Store - config.Configured - Shutdowner - Versioner - - Component(ComponentKey) Component - Components() ComponentStore -} - -var _ Core = (*Blas)(nil) - -type Shutdowner interface { - ShutdownBlas(context.Context) error -} - -type Versioner interface { - Version() string -} - type Blas struct { bus.Bus storage.Store auth.Authenticator Config *config.Config - components ComponentStore + components components.ComponentStore } -type ComponentStore map[ComponentKey]Component - func (b *Blas) Version() string { return common.Version } @@ -85,7 +64,7 @@ func (b *Blas) openStore() error { return err } -func (b *Blas) Component(k ComponentKey) Component { +func (b *Blas) Component(k components.ComponentKey) components.Component { c, ok := b.components[k] if !ok { return nil @@ -94,13 +73,13 @@ func (b *Blas) Component(k ComponentKey) Component { return c } -func (b *Blas) Components() ComponentStore { return b.components } +func (b *Blas) Components() components.ComponentStore { return b.components } func New(cfg *config.Config) (b *Blas, err error) { b = &Blas{ - Bus: bus.New(), - Config: cfg, - components: make(ComponentStore), + Bus: bus.New(), + Config: cfg, + components: make(components.ComponentStore), } err = b.openStore() @@ -109,5 +88,17 @@ func New(cfg *config.Config) (b *Blas, err error) { } b.Authenticator, err = auth.New(b.Store) + + for k, v := range Registry { + log.Info().Msgf("Setting up component %s", k) + c, err := v(b) + if err != nil { + log.Error().Err(err).Msgf("Error setting up component %s", k) + continue + } + + b.components[k] = c + } + return b, err } diff --git a/pkg/blas/components.go b/pkg/blas/components.go index 365525c..81b110c 100644 --- a/pkg/blas/components.go +++ b/pkg/blas/components.go @@ -2,21 +2,16 @@ package blas import ( "fmt" + + "dynatron.me/x/blasphem/pkg/blas/core" + "dynatron.me/x/blasphem/pkg/components" ) -type ( - Setup func(Core) (Component, error) - ComponentKey string +type Setup func(core.Core) (components.Component, error) - Component interface { - Shutdown() - } +var Registry = make(map[components.ComponentKey]Setup) -) - -var Registry = make(map[ComponentKey]Setup) - -func Register(key ComponentKey, c Setup) { +func Register(key components.ComponentKey, c Setup) { _, already := Registry[key] if already { panic(fmt.Sprintf("component %s already exists", key)) diff --git a/pkg/blas/core/core.go b/pkg/blas/core/core.go new file mode 100644 index 0000000..a7ee257 --- /dev/null +++ b/pkg/blas/core/core.go @@ -0,0 +1,29 @@ +package core + +import ( + "context" + + "dynatron.me/x/blasphem/pkg/auth" + "dynatron.me/x/blasphem/pkg/bus" + "dynatron.me/x/blasphem/pkg/components" + "dynatron.me/x/blasphem/pkg/config" + "dynatron.me/x/blasphem/pkg/storage" +) + +type Core interface { + auth.Authenticator + bus.Bus + storage.Store + config.Configured + Shutdowner + Versioner + components.Componenter +} + +type Shutdowner interface { + ShutdownBlas(context.Context) error +} + +type Versioner interface { + Version() string +} diff --git a/pkg/cmd/serve/cmd.go b/pkg/cmd/serve/cmd.go index 3908a28..02773bd 100644 --- a/pkg/cmd/serve/cmd.go +++ b/pkg/cmd/serve/cmd.go @@ -2,7 +2,7 @@ package serve import ( "dynatron.me/x/blasphem/internal/common" - "dynatron.me/x/blasphem/pkg/blas" + blas "dynatron.me/x/blasphem/pkg/blas/core" "dynatron.me/x/blasphem/pkg/server" "github.com/spf13/cobra" diff --git a/pkg/components/components.go b/pkg/components/components.go new file mode 100644 index 0000000..c7c24c8 --- /dev/null +++ b/pkg/components/components.go @@ -0,0 +1,17 @@ +package components + +import () + +type Componenter interface { + Component(ComponentKey) Component + Components() ComponentStore +} + +type ( + ComponentStore map[ComponentKey]Component + ComponentKey string + + Component interface { + Shutdown() + } +) diff --git a/pkg/frontend/frontend.go b/pkg/frontend/frontend.go index 824fd94..84f6a68 100644 --- a/pkg/frontend/frontend.go +++ b/pkg/frontend/frontend.go @@ -8,6 +8,8 @@ import ( "sync" "dynatron.me/x/blasphem/pkg/blas" + "dynatron.me/x/blasphem/pkg/blas/core" + "dynatron.me/x/blasphem/pkg/components" "github.com/labstack/echo/v4" ) @@ -27,6 +29,7 @@ type Frontend struct { func (fe *Frontend) InstallRoutes(e *echo.Echo) { fe.routeInstall.Do(func() { e.GET("/*", fe.fsHandler) + e.GET("/auth/authorize", fe.AliasHandler("authorize.html")) }) } @@ -49,7 +52,7 @@ func (fe *Frontend) AliasHandler(toFile string) echo.HandlerFunc { func (*Frontend) Shutdown() {} -func Setup(_ blas.Core) (blas.Component, error) { +func Setup(_ core.Core) (components.Component, error) { fe := &Frontend{} var err error diff --git a/pkg/server/server.go b/pkg/server/server.go index 0ef5e5d..df52989 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -13,12 +13,13 @@ import ( "github.com/ziflex/lecho/v3" "dynatron.me/x/blasphem/pkg/blas" + "dynatron.me/x/blasphem/pkg/blas/core" "dynatron.me/x/blasphem/pkg/frontend" conf "dynatron.me/x/blasphem/pkg/server/config" ) type Server struct { - blas.Core + core.Core *echo.Echo wg sync.WaitGroup } @@ -31,6 +32,7 @@ func (s *Server) installRoutes() { s.GET("/api/websocket", s.wsHandler) s.Component(frontend.FrontendKey).(RouteHaver).InstallRoutes(s.Echo) + s.Core.(*blas.Blas).Authenticator.InstallRoutes(s.Echo, s.Core) for _, c := range s.Components() { if rh, ok := c.(RouteHaver); ok { @@ -39,7 +41,7 @@ func (s *Server) installRoutes() { } } -func New(core blas.Core) (s *Server, err error) { +func New(core core.Core) (s *Server, err error) { s = &Server{ Core: core, Echo: echo.New(), diff --git a/pkg/wsapi/api.go b/pkg/wsapi/api.go index dff886f..bccd4dc 100644 --- a/pkg/wsapi/api.go +++ b/pkg/wsapi/api.go @@ -4,7 +4,7 @@ import ( "io" "dynatron.me/x/blasphem/pkg/auth" - "dynatron.me/x/blasphem/pkg/blas" + blas "dynatron.me/x/blasphem/pkg/blas/core" "github.com/gorilla/websocket" "github.com/labstack/echo/v4"