This commit is contained in:
Daniel Ponte 2022-12-20 16:26:04 -05:00
parent 1b355d3cbf
commit a1005ce6bf
9 changed files with 86 additions and 49 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"dynatron.me/x/blasphem/pkg/auth/provider" "dynatron.me/x/blasphem/pkg/auth/provider"
"dynatron.me/x/blasphem/pkg/components"
"dynatron.me/x/blasphem/pkg/storage" "dynatron.me/x/blasphem/pkg/storage"
// providers // providers
@ -34,6 +35,7 @@ type authenticator struct {
type Authenticator interface { type Authenticator interface {
ValidateAccessToken(token AccessToken) *RefreshToken ValidateAccessToken(token AccessToken) *RefreshToken
InstallRoutes(e *echo.Echo, comp components.Componenter)
} }
type AuthError struct { type AuthError struct {
@ -41,10 +43,8 @@ type AuthError struct {
Description string `json:"error_description"` 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") authG := e.Group("/auth")
panic("reinstall authorize")
//authG.GET("/authorize", frontend.AliasHandler("authorize.html"))
authG.GET("/providers", a.ProvidersHandler) authG.GET("/providers", a.ProvidersHandler)
authG.POST("/token", a.TokenHandler) authG.POST("/token", a.TokenHandler)

View file

@ -9,43 +9,22 @@ import (
"dynatron.me/x/blasphem/internal/common" "dynatron.me/x/blasphem/internal/common"
"dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/auth"
"dynatron.me/x/blasphem/pkg/bus" "dynatron.me/x/blasphem/pkg/bus"
"dynatron.me/x/blasphem/pkg/components"
"dynatron.me/x/blasphem/pkg/config" "dynatron.me/x/blasphem/pkg/config"
"dynatron.me/x/blasphem/pkg/storage" "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 { type Blas struct {
bus.Bus bus.Bus
storage.Store storage.Store
auth.Authenticator auth.Authenticator
Config *config.Config Config *config.Config
components ComponentStore components components.ComponentStore
} }
type ComponentStore map[ComponentKey]Component
func (b *Blas) Version() string { func (b *Blas) Version() string {
return common.Version return common.Version
} }
@ -85,7 +64,7 @@ func (b *Blas) openStore() error {
return err return err
} }
func (b *Blas) Component(k ComponentKey) Component { func (b *Blas) Component(k components.ComponentKey) components.Component {
c, ok := b.components[k] c, ok := b.components[k]
if !ok { if !ok {
return nil return nil
@ -94,13 +73,13 @@ func (b *Blas) Component(k ComponentKey) Component {
return c 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) { func New(cfg *config.Config) (b *Blas, err error) {
b = &Blas{ b = &Blas{
Bus: bus.New(), Bus: bus.New(),
Config: cfg, Config: cfg,
components: make(ComponentStore), components: make(components.ComponentStore),
} }
err = b.openStore() err = b.openStore()
@ -109,5 +88,17 @@ func New(cfg *config.Config) (b *Blas, err error) {
} }
b.Authenticator, err = auth.New(b.Store) 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 return b, err
} }

View file

@ -2,21 +2,16 @@ package blas
import ( import (
"fmt" "fmt"
"dynatron.me/x/blasphem/pkg/blas/core"
"dynatron.me/x/blasphem/pkg/components"
) )
type ( type Setup func(core.Core) (components.Component, error)
Setup func(Core) (Component, error)
ComponentKey string
Component interface { var Registry = make(map[components.ComponentKey]Setup)
Shutdown()
}
) func Register(key components.ComponentKey, c Setup) {
var Registry = make(map[ComponentKey]Setup)
func Register(key ComponentKey, c Setup) {
_, already := Registry[key] _, already := Registry[key]
if already { if already {
panic(fmt.Sprintf("component %s already exists", key)) panic(fmt.Sprintf("component %s already exists", key))

29
pkg/blas/core/core.go Normal file
View file

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

View file

@ -2,7 +2,7 @@ package serve
import ( import (
"dynatron.me/x/blasphem/internal/common" "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" "dynatron.me/x/blasphem/pkg/server"
"github.com/spf13/cobra" "github.com/spf13/cobra"

View file

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

View file

@ -8,6 +8,8 @@ import (
"sync" "sync"
"dynatron.me/x/blasphem/pkg/blas" "dynatron.me/x/blasphem/pkg/blas"
"dynatron.me/x/blasphem/pkg/blas/core"
"dynatron.me/x/blasphem/pkg/components"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -27,6 +29,7 @@ type Frontend struct {
func (fe *Frontend) InstallRoutes(e *echo.Echo) { func (fe *Frontend) InstallRoutes(e *echo.Echo) {
fe.routeInstall.Do(func() { fe.routeInstall.Do(func() {
e.GET("/*", fe.fsHandler) 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 (*Frontend) Shutdown() {}
func Setup(_ blas.Core) (blas.Component, error) { func Setup(_ core.Core) (components.Component, error) {
fe := &Frontend{} fe := &Frontend{}
var err error var err error

View file

@ -13,12 +13,13 @@ import (
"github.com/ziflex/lecho/v3" "github.com/ziflex/lecho/v3"
"dynatron.me/x/blasphem/pkg/blas" "dynatron.me/x/blasphem/pkg/blas"
"dynatron.me/x/blasphem/pkg/blas/core"
"dynatron.me/x/blasphem/pkg/frontend" "dynatron.me/x/blasphem/pkg/frontend"
conf "dynatron.me/x/blasphem/pkg/server/config" conf "dynatron.me/x/blasphem/pkg/server/config"
) )
type Server struct { type Server struct {
blas.Core core.Core
*echo.Echo *echo.Echo
wg sync.WaitGroup wg sync.WaitGroup
} }
@ -31,6 +32,7 @@ func (s *Server) installRoutes() {
s.GET("/api/websocket", s.wsHandler) s.GET("/api/websocket", s.wsHandler)
s.Component(frontend.FrontendKey).(RouteHaver).InstallRoutes(s.Echo) s.Component(frontend.FrontendKey).(RouteHaver).InstallRoutes(s.Echo)
s.Core.(*blas.Blas).Authenticator.InstallRoutes(s.Echo, s.Core)
for _, c := range s.Components() { for _, c := range s.Components() {
if rh, ok := c.(RouteHaver); ok { 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{ s = &Server{
Core: core, Core: core,
Echo: echo.New(), Echo: echo.New(),

View file

@ -4,7 +4,7 @@ import (
"io" "io"
"dynatron.me/x/blasphem/pkg/auth" "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/gorilla/websocket"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"