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"
"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)

View file

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

View file

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

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 (
"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"

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"
"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

View file

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

View file

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