visedriver/internal/storage/storageservice.go

175 lines
4.2 KiB
Go
Raw Normal View History

2024-09-19 14:57:11 +02:00
package storage
import (
"context"
"fmt"
"os"
"path"
2024-09-19 14:57:11 +02:00
"git.defalsify.org/vise.git/db"
fsdb "git.defalsify.org/vise.git/db/fs"
2024-10-19 22:06:58 +02:00
"git.defalsify.org/vise.git/db/postgres"
2025-01-02 10:39:49 +01:00
"git.defalsify.org/vise.git/lang"
"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"
2025-01-04 09:09:18 +01:00
gdbmstorage "git.grassecon.net/urdt/ussd/internal/storage/db/gdbm"
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)
2024-09-19 14:57:11 +02:00
}
type MenuStorageService struct {
conn ConnData
resourceDir string
2025-01-02 10:39:49 +01:00
poResource resource.Resource
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 NewMenuStorageService(conn ConnData, resourceDir string) *MenuStorageService {
return &MenuStorageService{
conn: conn,
resourceDir: resourceDir,
}
}
2025-01-04 23:27:46 +01:00
func (ms *MenuStorageService) WithResourceDir(resourceDir string) *MenuStorageService {
ms.resourceDir = resourceDir
return ms
}
func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) {
2025-01-04 10:37:12 +01:00
var newDb db.Db
var err error
2024-10-19 14:27:23 +02:00
if existingDb != nil {
return existingDb, nil
}
connStr := ms.conn.String()
2025-01-04 10:37:12 +01:00
dbTyp := ms.conn.DbType()
if dbTyp == DBTYPE_POSTGRES {
2024-10-19 22:06:58 +02:00
newDb = postgres.NewPgDb()
2025-01-04 10:37:12 +01:00
} else if dbTyp == DBTYPE_GDBM {
err = ms.ensureDbDir()
if err != nil {
return nil, err
}
connStr = path.Join(connStr, section)
2025-01-04 08:35:28 +01:00
newDb = gdbmstorage.NewThreadGdbmDb()
2025-01-04 10:37:12 +01:00
} else {
2025-01-04 21:52:49 +01:00
return nil, fmt.Errorf("unsupported connection string: '%s'\n", ms.conn.String())
2024-10-19 14:27:23 +02:00
}
logg.DebugCtxf(ctx, "connecting to db", "conn", connStr)
err = newDb.Connect(ctx, connStr)
2024-10-19 14:27:23 +02:00
if err != nil {
return nil, err
}
return newDb, nil
}
2025-01-02 19:13:37 +01:00
// WithGettext triggers use of gettext for translation of templates and menus.
//
// The first language in `lns` will be used as default language, to resolve node keys to
// language strings.
//
// If `lns` is an empty array, gettext will not be used.
2025-01-02 10:39:49 +01:00
func (ms *MenuStorageService) WithGettext(path string, lns []lang.Language) *MenuStorageService {
2025-01-02 19:13:37 +01:00
if len(lns) == 0 {
logg.Warnf("Gettext requested but no languages supplied")
return ms
2025-01-02 10:39:49 +01:00
}
2025-01-02 19:13:37 +01:00
rs := resource.NewPoResource(lns[0], path)
2025-01-02 10:39:49 +01:00
2025-01-02 19:13:37 +01:00
for _, ln := range(lns) {
2025-01-02 10:39:49 +01:00
rs = rs.WithLanguage(ln)
}
ms.poResource = rs
return ms
}
func (ms *MenuStorageService) GetPersister(ctx context.Context) (*persist.Persister, error) {
2024-10-19 14:27:23 +02:00
stateStore, err := ms.GetStateStore(ctx)
2024-09-21 22:32:02 +02:00
if err != nil {
return nil, err
}
2024-10-19 14:27:23 +02:00
pr := persist.NewPersister(stateStore)
logg.TraceCtxf(ctx, "menu storage service", "persist", pr, "store", 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) {
2024-10-19 14:27:23 +02:00
if ms.userDataStore != nil {
return ms.userDataStore, nil
2024-09-21 22:32:02 +02:00
}
2024-10-19 14:27:23 +02:00
userDataStore, err := ms.getOrCreateDb(ctx, ms.userDataStore, "userdata.gdbm")
if err != nil {
return nil, err
}
2024-10-19 14:27:23 +02:00
ms.userDataStore = userDataStore
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)
2025-01-02 10:39:49 +01:00
if ms.poResource != nil {
2025-01-02 19:13:37 +01:00
logg.InfoCtxf(ctx, "using poresource for menu and template")
2025-01-02 10:39:49 +01:00
rfs.WithMenuGetter(ms.poResource.GetMenu)
rfs.WithTemplateGetter(ms.poResource.GetTemplate)
}
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 {
2024-10-19 14:27:23 +02:00
return ms.stateStore, nil
2024-09-21 22:32:02 +02:00
}
2024-10-19 14:27:23 +02:00
stateStore, err := ms.getOrCreateDb(ctx, ms.stateStore, "state.gdbm")
2024-09-21 22:32:02 +02:00
if err != nil {
return nil, err
}
2024-10-19 14:27:23 +02:00
ms.stateStore = stateStore
2024-09-21 22:32:02 +02:00
return ms.stateStore, nil
2024-09-19 14:57:11 +02:00
}
2025-01-04 10:37:12 +01:00
func (ms *MenuStorageService) ensureDbDir() error {
err := os.MkdirAll(ms.conn.String(), 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
}