1
.gitignore
vendored
1
.gitignore
vendored
@@ -19,3 +19,4 @@ ui/
|
||||
frontend/.pnp.cjs
|
||||
frontend/.pnp.loader.mjs
|
||||
frontend/.yarn/install-state.gz
|
||||
.db.lock
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -20,6 +21,7 @@ type Config struct {
|
||||
Password string `yaml:"password"`
|
||||
QueueSize int `yaml:"queue_size"`
|
||||
SessionFilePath string `yaml:"session_file_path"`
|
||||
path string
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -43,9 +45,17 @@ func (c *Config) LoadFile(filename string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.path = filename
|
||||
|
||||
if err := yaml.NewDecoder(fd).Decode(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Path of the directory containing the config file
|
||||
func (c *Config) Dir() string { return filepath.Dir(c.path) }
|
||||
|
||||
// Absolute path of the config file
|
||||
func (c *Config) Path() string { return c.path }
|
||||
|
||||
@@ -3,29 +3,41 @@ package dbutil
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
|
||||
)
|
||||
|
||||
var lockFilePath = filepath.Join(config.Instance().Dir(), ".db.lock")
|
||||
|
||||
// Run the table migration
|
||||
func AutoMigrate(ctx context.Context, db *sql.DB) error {
|
||||
func Migrate(ctx context.Context, db *sql.DB) error {
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
defer func() {
|
||||
conn.Close()
|
||||
createLockFile()
|
||||
}()
|
||||
|
||||
_, err = db.ExecContext(
|
||||
if _, err := db.ExecContext(
|
||||
ctx,
|
||||
`CREATE TABLE IF NOT EXISTS templates (
|
||||
id CHAR(36) PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
content TEXT NOT NULL
|
||||
)`,
|
||||
)
|
||||
if err != nil {
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if lockFileExists() {
|
||||
return nil
|
||||
}
|
||||
|
||||
db.ExecContext(
|
||||
ctx,
|
||||
`INSERT INTO templates (id, name, content) VALUES
|
||||
@@ -35,5 +47,12 @@ func AutoMigrate(ctx context.Context, db *sql.DB) error {
|
||||
"1", "audio only", "-x",
|
||||
)
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func createLockFile() { os.Create(lockFilePath) }
|
||||
|
||||
func lockFileExists() bool {
|
||||
_, err := os.Stat(lockFilePath)
|
||||
return os.IsExist(err)
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ func RunBlocking(cfg *RunConfig) {
|
||||
logger.Error("failed to open database", slog.String("err", err.Error()))
|
||||
}
|
||||
|
||||
if err := dbutil.AutoMigrate(context.Background(), db); err != nil {
|
||||
if err := dbutil.Migrate(context.Background(), db); err != nil {
|
||||
logger.Error("failed to init database", slog.String("err", err.Error()))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user