blasphem/pkg/blas/blas.go
2022-12-19 19:24:01 -05:00

86 lines
1.4 KiB
Go

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"
"dynatron.me/x/blasphem/pkg/storage"
)
type Core interface {
auth.Authenticator
bus.Bus
storage.Store
config.Configured
Shutdowner
Versioner
}
type Shutdowner interface {
Shutdown(context.Context) error
}
type Versioner interface {
Version() string
}
type Blas struct {
bus.Bus
storage.Store
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) (*Blas, error) {
b := &Blas{
Bus: bus.New(),
Config: cfg,
}
err := b.openStore()
return b, err
}