nsfwapp/backend/model_store_config.go
2026-06-23 14:38:06 +02:00

61 lines
1.4 KiB
Go

package main
import (
"path/filepath"
"strings"
)
const (
databaseTypeSQLite = "sqlite"
databaseTypePostgres = "postgres"
defaultSQLitePath = "nsfwapp.sqlite"
)
type ModelStoreConfig struct {
Type string
DSN string
SQLitePath string
}
func normalizeDatabaseType(raw string) string {
switch strings.ToLower(strings.TrimSpace(raw)) {
case databaseTypePostgres:
return databaseTypePostgres
default:
return databaseTypeSQLite
}
}
func normalizeSQLitePath(raw string) string {
value := strings.TrimSpace(raw)
if value == "" {
value = defaultSQLitePath
}
return filepath.Clean(filepath.FromSlash(value))
}
func buildModelStoreConfigFromSettings() (ModelStoreConfig, error) {
return buildModelStoreConfigFromRecorderSettings(getSettings())
}
func buildModelStoreConfigFromRecorderSettings(s RecorderSettings) (ModelStoreConfig, error) {
dbType := normalizeDatabaseType(s.DatabaseType)
if dbType == databaseTypePostgres {
dsn, err := buildPostgresDSNFromRecorderSettings(s)
if err != nil {
return ModelStoreConfig{}, err
}
return ModelStoreConfig{Type: databaseTypePostgres, DSN: dsn}, nil
}
sqlitePath, err := resolvePathRelativeToApp(normalizeSQLitePath(s.SQLitePath))
if err != nil {
return ModelStoreConfig{}, err
}
return ModelStoreConfig{
Type: databaseTypeSQLite,
DSN: sqlitePath,
SQLitePath: sqlitePath,
}, nil
}