blasphem/pkg/blas/blas.go

107 lines
1.9 KiB
Go
Raw Normal View History

2022-10-28 18:19:55 -04:00
package blas
import (
"context"
2022-12-20 13:54:49 -05:00
"os"
"path"
"strings"
"dynatron.me/x/blasphem/internal/common"
2022-12-19 19:24:01 -05:00
"dynatron.me/x/blasphem/pkg/auth"
2022-12-20 19:05:45 -05:00
"dynatron.me/x/blasphem/pkg/blas/core"
2022-10-28 18:19:55 -04:00
"dynatron.me/x/blasphem/pkg/bus"
2022-12-20 16:26:04 -05:00
"dynatron.me/x/blasphem/pkg/components"
2022-10-28 18:19:55 -04:00
"dynatron.me/x/blasphem/pkg/config"
"dynatron.me/x/blasphem/pkg/storage"
2022-12-20 13:54:49 -05:00
2022-12-20 16:26:04 -05:00
"github.com/rs/zerolog/log"
)
2022-12-20 13:54:49 -05:00
type Blas struct {
bus.Bus
storage.Store
auth.Authenticator
Config *config.Config
2022-12-20 19:05:45 -05:00
core.WebSocketManager
2022-12-20 13:54:49 -05:00
2022-12-20 16:26:04 -05:00
components components.ComponentStore
2022-12-20 13:54:49 -05:00
}
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
}
2022-12-20 16:26:04 -05:00
func (b *Blas) Component(k components.ComponentKey) components.Component {
2022-12-20 13:54:49 -05:00
c, ok := b.components[k]
if !ok {
return nil
}
return c
}
2022-12-20 16:26:04 -05:00
func (b *Blas) Components() components.ComponentStore { return b.components }
2022-12-20 13:54:49 -05:00
func New(cfg *config.Config) (b *Blas, err error) {
b = &Blas{
2022-12-20 16:26:04 -05:00
Bus: bus.New(),
Config: cfg,
components: make(components.ComponentStore),
2022-12-20 13:54:49 -05:00
}
err = b.openStore()
if err != nil {
return nil, err
}
b.Authenticator, err = auth.New(b.Store)
2022-12-20 16:26:04 -05:00
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
}
2022-12-20 13:54:49 -05:00
return b, err
}