ussd/internal/storage/storageservice.go

133 lines
3.3 KiB
Go
Raw Normal View History

2024-09-19 14:57:11 +02:00
package storage
import (
"context"
"fmt"
"os"
"path"
"git.defalsify.org/vise.git/db"
fsdb "git.defalsify.org/vise.git/db/fs"
"git.defalsify.org/vise.git/logging"
2024-09-19 14:57:11 +02:00
"git.defalsify.org/vise.git/persist"
"git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/initializers"
2024-09-19 14:57:11 +02:00
)
2024-09-21 22:32:02 +02:00
var (
logg = logging.NewVanilla().WithDomain("storage")
)
2024-09-21 22:32:02 +02:00
2024-09-19 14:57:11 +02:00
type StorageService interface {
GetPersister(ctx context.Context) (*persist.Persister, error)
GetUserdataDb(ctx context.Context) db.Db
GetResource(ctx context.Context) (resource.Resource, error)
EnsureDbDir() error
2024-09-19 14:57:11 +02:00
}
type MenuStorageService struct {
dbDir string
resourceDir string
2024-09-21 22:32:02 +02:00
resourceStore db.Db
stateStore db.Db
2024-09-21 22:32:02 +02:00
userDataStore db.Db
}
2024-09-19 14:57:11 +02:00
func buildConnStr() string {
host := initializers.GetEnv("DB_HOST", "localhost")
user := initializers.GetEnv("DB_USER", "postgres")
password := initializers.GetEnv("DB_PASSWORD", "")
dbName := initializers.GetEnv("DB_NAME", "")
port := initializers.GetEnv("DB_PORT", "5432")
return fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s",
user, password, host, port, dbName,
)
}
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
return &MenuStorageService{
dbDir: dbDir,
resourceDir: resourceDir,
}
}
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
ms.stateStore = NewThreadGdbmDb()
storeFile := path.Join(ms.dbDir, "state.gdbm")
2024-09-21 22:32:02 +02:00
err := ms.stateStore.Connect(ctx, storeFile)
if err != nil {
return nil, err
}
pr := persist.NewPersister(ms.stateStore)
logg.TraceCtxf(ctx, "menu storage service", "persist", pr, "store", ms.stateStore)
2024-09-19 14:57:11 +02:00
return pr, nil
}
2024-09-21 22:32:02 +02:00
func (ms *MenuStorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
database, ok := ctx.Value("Database").(string)
if !ok {
2024-10-17 11:55:47 +02:00
return nil, fmt.Errorf("failed to select the database")
2024-09-21 22:32:02 +02:00
}
if database == "postgres" {
ms.userDataStore = NewThreadPostgresDb()
connStr := buildConnStr()
err := ms.userDataStore.Connect(ctx, connStr)
if err != nil {
return nil, err
}
} else {
ms.userDataStore = NewThreadGdbmDb()
storeFile := path.Join(ms.dbDir, "userdata.gdbm")
err := ms.userDataStore.Connect(ctx, storeFile)
if err != nil {
return nil, err
}
}
2024-09-21 22:32:02 +02:00
return ms.userDataStore, nil
2024-09-19 14:57:11 +02:00
}
func (ms *MenuStorageService) GetResource(ctx context.Context) (resource.Resource, error) {
2024-09-21 22:32:02 +02:00
ms.resourceStore = fsdb.NewFsDb()
err := ms.resourceStore.Connect(ctx, ms.resourceDir)
2024-09-19 14:57:11 +02:00
if err != nil {
return nil, err
}
2024-09-21 22:32:02 +02:00
rfs := resource.NewDbResource(ms.resourceStore)
2024-09-19 14:57:11 +02:00
return rfs, nil
}
func (ms *MenuStorageService) GetStateStore(ctx context.Context) (db.Db, error) {
2024-09-21 22:32:02 +02:00
if ms.stateStore != nil {
panic("set up store when already exists")
}
ms.stateStore = NewThreadGdbmDb()
storeFile := path.Join(ms.dbDir, "state.gdbm")
2024-09-21 22:32:02 +02:00
err := ms.stateStore.Connect(ctx, storeFile)
if err != nil {
return nil, err
}
return ms.stateStore, nil
2024-09-19 14:57:11 +02:00
}
func (ms *MenuStorageService) EnsureDbDir() error {
err := os.MkdirAll(ms.dbDir, 0700)
2024-09-19 14:57:11 +02:00
if err != nil {
return fmt.Errorf("state dir create exited with error: %v\n", err)
}
return nil
}
2024-09-21 22:32:02 +02:00
func (ms *MenuStorageService) Close() error {
errA := ms.stateStore.Close()
errB := ms.userDataStore.Close()
errC := ms.resourceStore.Close()
if errA != nil || errB != nil || errC != nil {
return fmt.Errorf("%v %v %v", errA, errB, errC)
}
return nil
}