also compiles

This commit is contained in:
Daniel Ponte 2022-12-20 13:54:49 -05:00
parent 0da222577b
commit 1b355d3cbf
5 changed files with 105 additions and 80 deletions

View file

@ -7,7 +7,7 @@ import (
"github.com/rs/zerolog/log"
"dynatron.me/x/blasphem/internal/common"
"dynatron.me/x/blasphem/pkg/blas/core"
"dynatron.me/x/blasphem/pkg/blas"
"dynatron.me/x/blasphem/pkg/cmd/serve"
"dynatron.me/x/blasphem/pkg/config"
@ -26,7 +26,7 @@ func main() {
log.Fatal().Err(err).Msg("Config read failed")
}
bl, err := core.New(config)
bl, err := blas.New(config)
if err != nil {
log.Fatal().Err(err).Msg("Core create failed")
}

View file

@ -2,6 +2,11 @@ package blas
import (
"context"
"os"
"path"
"strings"
"dynatron.me/x/blasphem/internal/common"
"dynatron.me/x/blasphem/pkg/auth"
"dynatron.me/x/blasphem/pkg/bus"
"dynatron.me/x/blasphem/pkg/config"
@ -15,8 +20,13 @@ type Core interface {
config.Configured
Shutdowner
Versioner
Component(ComponentKey) Component
Components() ComponentStore
}
var _ Core = (*Blas)(nil)
type Shutdowner interface {
ShutdownBlas(context.Context) error
}
@ -24,3 +34,80 @@ type Shutdowner interface {
type Versioner interface {
Version() string
}
type Blas struct {
bus.Bus
storage.Store
auth.Authenticator
Config *config.Config
components ComponentStore
}
type ComponentStore map[ComponentKey]Component
func (b *Blas) Version() string {
return common.Version
}
func (b *Blas) Conf() *config.Config { return b.Config }
func (b *Blas) ShutdownBlas(ctx context.Context) error {
b.Bus.ShutdownBus()
b.Store.ShutdownStore()
return ctx.Err()
}
func (b *Blas) ConfigDir() (cd string) {
if b.Config.DataDir != nil {
cd = *b.Config.DataDir
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
switch {
case cd == "":
return path.Join(home, "."+common.AppName)
case strings.HasPrefix(cd, "~/"):
return path.Join(home, cd[2:])
default:
return cd
}
}
func (b *Blas) openStore() error {
// TODO: based on config, open filestore or db store
stor, err := storage.OpenFileStore(b.ConfigDir())
b.Store = stor
return err
}
func (b *Blas) Component(k ComponentKey) Component {
c, ok := b.components[k]
if !ok {
return nil
}
return c
}
func (b *Blas) Components() ComponentStore { return b.components }
func New(cfg *config.Config) (b *Blas, err error) {
b = &Blas{
Bus: bus.New(),
Config: cfg,
components: make(ComponentStore),
}
err = b.openStore()
if err != nil {
return nil, err
}
b.Authenticator, err = auth.New(b.Store)
return b, err
}

View file

@ -6,7 +6,7 @@ import (
type (
Setup func(Core) (Component, error)
Key string
ComponentKey string
Component interface {
Shutdown()
@ -14,9 +14,9 @@ type (
)
var Registry = make(map[Key]Setup)
var Registry = make(map[ComponentKey]Setup)
func Register(key Key, c Setup) {
func Register(key ComponentKey, c Setup) {
_, already := Registry[key]
if already {
panic(fmt.Sprintf("component %s already exists", key))

View file

@ -1,75 +0,0 @@
package core
import (
"context"
"os"
"path"
"strings"
"dynatron.me/x/blasphem/internal/common"
"dynatron.me/x/blasphem/pkg/auth"
"dynatron.me/x/blasphem/pkg/bus"
"dynatron.me/x/blasphem/pkg/config"
"dynatron.me/x/blasphem/pkg/storage"
)
type Blas struct {
bus.Bus
storage.Store
auth.Authenticator
Config *config.Config
}
func (b *Blas) Version() string {
return common.Version
}
func (b *Blas) Conf() *config.Config { return b.Config }
func (b *Blas) ShutdownBlas(ctx context.Context) error {
b.Bus.ShutdownBus()
b.Store.ShutdownStore()
return ctx.Err()
}
func (b *Blas) ConfigDir() (cd string) {
if b.Config.DataDir != nil {
cd = *b.Config.DataDir
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
switch {
case cd == "":
return path.Join(home, "."+common.AppName)
case strings.HasPrefix(cd, "~/"):
return path.Join(home, cd[2:])
default:
return cd
}
}
func (b *Blas) openStore() error {
// TODO: based on config, open filestore or db store
stor, err := storage.OpenFileStore(b.ConfigDir())
b.Store = stor
return err
}
func New(cfg *config.Config) (b *Blas, err error) {
b = &Blas{
Bus: bus.New(),
Config: cfg,
}
err = b.openStore()
if err != nil {
return nil, err
}
b.Authenticator, err = auth.New(b.Store)
return b, err
}

View file

@ -13,6 +13,7 @@ import (
"github.com/ziflex/lecho/v3"
"dynatron.me/x/blasphem/pkg/blas"
"dynatron.me/x/blasphem/pkg/frontend"
conf "dynatron.me/x/blasphem/pkg/server/config"
)
@ -22,8 +23,20 @@ type Server struct {
wg sync.WaitGroup
}
type RouteHaver interface {
InstallRoutes(e *echo.Echo)
}
func (s *Server) installRoutes() {
s.GET("/api/websocket", s.wsHandler)
s.Component(frontend.FrontendKey).(RouteHaver).InstallRoutes(s.Echo)
for _, c := range s.Components() {
if rh, ok := c.(RouteHaver); ok {
rh.InstallRoutes(s.Echo)
}
}
}
func New(core blas.Core) (s *Server, err error) {