Store local DB in user state dir as qfs.db
This commit is contained in:
13
README.md
13
README.md
@@ -120,6 +120,17 @@ make help # Показать все команды
|
|||||||
|
|
||||||
Приложение будет доступно по адресу: http://localhost:8080
|
Приложение будет доступно по адресу: http://localhost:8080
|
||||||
|
|
||||||
|
### Локальная SQLite база (state)
|
||||||
|
|
||||||
|
Локальная база приложения хранится в профиле пользователя и не зависит от расположения бинарника.
|
||||||
|
Имя файла: `qfs.db`.
|
||||||
|
|
||||||
|
- macOS: `~/Library/Application Support/QuoteForge/qfs.db`
|
||||||
|
- Linux: `$XDG_STATE_HOME/quoteforge/qfs.db` (или `~/.local/state/quoteforge/qfs.db`)
|
||||||
|
- Windows: `%LOCALAPPDATA%\\QuoteForge\\qfs.db`
|
||||||
|
|
||||||
|
Можно переопределить путь через `-localdb` или переменную окружения `QFS_DB_PATH`.
|
||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -245,6 +256,8 @@ CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/qfs ./cmd/qfs
|
|||||||
| `QF_DB_PASSWORD` | Пароль БД | — |
|
| `QF_DB_PASSWORD` | Пароль БД | — |
|
||||||
| `QF_JWT_SECRET` | Секрет для JWT | — |
|
| `QF_JWT_SECRET` | Секрет для JWT | — |
|
||||||
| `QF_SERVER_PORT` | Порт сервера | 8080 |
|
| `QF_SERVER_PORT` | Порт сервера | 8080 |
|
||||||
|
| `QFS_DB_PATH` | Полный путь к локальной SQLite БД | OS-specific user state dir |
|
||||||
|
| `QFS_STATE_DIR` | Каталог state (если `QFS_DB_PATH` не задан) | OS-specific user state dir |
|
||||||
|
|
||||||
## Интеграция с существующей БД
|
## Интеграция с существующей БД
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.mchus.pro/mchus/quoteforge/internal/appstate"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/config"
|
"git.mchus.pro/mchus/quoteforge/internal/config"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/models"
|
"git.mchus.pro/mchus/quoteforge/internal/models"
|
||||||
@@ -16,7 +17,11 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configPath := flag.String("config", "config.yaml", "path to config file")
|
configPath := flag.String("config", "config.yaml", "path to config file")
|
||||||
localDBPath := flag.String("localdb", "./data/settings.db", "path to local SQLite database")
|
defaultLocalDBPath, err := appstate.ResolveDBPath("")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to resolve default local SQLite path: %v", err)
|
||||||
|
}
|
||||||
|
localDBPath := flag.String("localdb", defaultLocalDBPath, "path to local SQLite database (default: user state dir or QFS_DB_PATH)")
|
||||||
dryRun := flag.Bool("dry-run", false, "show what would be migrated without actually doing it")
|
dryRun := flag.Bool("dry-run", false, "show what would be migrated without actually doing it")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
qfassets "git.mchus.pro/mchus/quoteforge"
|
qfassets "git.mchus.pro/mchus/quoteforge"
|
||||||
|
"git.mchus.pro/mchus/quoteforge/internal/appstate"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/config"
|
"git.mchus.pro/mchus/quoteforge/internal/config"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/db"
|
"git.mchus.pro/mchus/quoteforge/internal/db"
|
||||||
"git.mchus.pro/mchus/quoteforge/internal/handlers"
|
"git.mchus.pro/mchus/quoteforge/internal/handlers"
|
||||||
@@ -34,15 +35,12 @@ import (
|
|||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
localDBPath = "./data/settings.db"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Version is set via ldflags during build
|
// Version is set via ldflags during build
|
||||||
var Version = "dev"
|
var Version = "dev"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configPath := flag.String("config", "config.yaml", "path to config file (optional, for server settings)")
|
configPath := flag.String("config", "config.yaml", "path to config file (optional, for server settings)")
|
||||||
|
localDBPath := flag.String("localdb", "", "path to local SQLite database (default: user state dir or QFS_DB_PATH)")
|
||||||
migrate := flag.Bool("migrate", false, "run database migrations")
|
migrate := flag.Bool("migrate", false, "run database migrations")
|
||||||
version := flag.Bool("version", false, "show version information")
|
version := flag.Bool("version", false, "show version information")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -53,8 +51,28 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolvedLocalDBPath, err := appstate.ResolveDBPath(*localDBPath)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to resolve local database path", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrate legacy project-local DB path to the user state directory when using defaults.
|
||||||
|
if *localDBPath == "" && os.Getenv("QFS_DB_PATH") == "" {
|
||||||
|
legacyPaths := []string{
|
||||||
|
filepath.Join("data", "settings.db"),
|
||||||
|
filepath.Join("data", "qfs.db"),
|
||||||
|
}
|
||||||
|
migratedFrom, migrateErr := appstate.MigrateLegacyDB(resolvedLocalDBPath, legacyPaths)
|
||||||
|
if migrateErr != nil {
|
||||||
|
slog.Warn("failed to migrate legacy local database", "error", migrateErr)
|
||||||
|
} else if migratedFrom != "" {
|
||||||
|
slog.Info("migrated legacy local database", "from", migratedFrom, "to", resolvedLocalDBPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize local SQLite database (always used)
|
// Initialize local SQLite database (always used)
|
||||||
local, err := localdb.New(localDBPath)
|
local, err := localdb.New(resolvedLocalDBPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to initialize local database", "error", err)
|
slog.Error("failed to initialize local database", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
144
internal/appstate/path.go
Normal file
144
internal/appstate/path.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package appstate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
appDirName = "QuoteForge"
|
||||||
|
defaultDB = "qfs.db"
|
||||||
|
envDBPath = "QFS_DB_PATH"
|
||||||
|
envStateDir = "QFS_STATE_DIR"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ResolveDBPath returns the local SQLite path using priority:
|
||||||
|
// explicit CLI path > QFS_DB_PATH > OS-specific user state directory.
|
||||||
|
func ResolveDBPath(explicitPath string) (string, error) {
|
||||||
|
if explicitPath != "" {
|
||||||
|
return filepath.Clean(explicitPath), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if fromEnv := os.Getenv(envDBPath); fromEnv != "" {
|
||||||
|
return filepath.Clean(fromEnv), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, err := defaultStateDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(dir, defaultDB), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MigrateLegacyDB copies an existing legacy DB (and optional SQLite sidecars)
|
||||||
|
// to targetPath if targetPath does not already exist.
|
||||||
|
// Returns source path if migration happened.
|
||||||
|
func MigrateLegacyDB(targetPath string, legacyPaths []string) (string, error) {
|
||||||
|
if targetPath == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists(targetPath) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
|
||||||
|
return "", fmt.Errorf("creating target db directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, src := range legacyPaths {
|
||||||
|
if src == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
src = filepath.Clean(src)
|
||||||
|
if src == targetPath || !exists(src) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := copyFile(src, targetPath); err != nil {
|
||||||
|
return "", fmt.Errorf("migrating legacy db from %s: %w", src, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional SQLite sidecar files.
|
||||||
|
_ = copyIfExists(src+"-wal", targetPath+"-wal")
|
||||||
|
_ = copyIfExists(src+"-shm", targetPath+"-shm")
|
||||||
|
|
||||||
|
return src, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultStateDir() (string, error) {
|
||||||
|
if override := os.Getenv(envStateDir); override != "" {
|
||||||
|
return filepath.Clean(override), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "darwin":
|
||||||
|
base, err := os.UserConfigDir() // ~/Library/Application Support
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("resolving user config dir: %w", err)
|
||||||
|
}
|
||||||
|
return filepath.Join(base, appDirName), nil
|
||||||
|
case "windows":
|
||||||
|
if local := os.Getenv("LOCALAPPDATA"); local != "" {
|
||||||
|
return filepath.Join(local, appDirName), nil
|
||||||
|
}
|
||||||
|
base, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("resolving user config dir: %w", err)
|
||||||
|
}
|
||||||
|
return filepath.Join(base, appDirName), nil
|
||||||
|
default:
|
||||||
|
if xdgState := os.Getenv("XDG_STATE_HOME"); xdgState != "" {
|
||||||
|
return filepath.Join(xdgState, "quoteforge"), nil
|
||||||
|
}
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("resolving user home dir: %w", err)
|
||||||
|
}
|
||||||
|
return filepath.Join(home, ".local", "state", "quoteforge"), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func exists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyIfExists(src, dst string) error {
|
||||||
|
if !exists(src) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return copyFile(src, dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyFile(src, dst string) error {
|
||||||
|
in, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
|
||||||
|
info, err := in.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode().Perm())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
if _, err := io.Copy(out, in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.Sync()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user