feat: auto-create database directory if missing (#510)

This commit is contained in:
Modestas Rinkevičius
2025-12-11 14:43:57 +02:00
committed by GitHub
parent 5f2ec02c3d
commit 3961589f1e
3 changed files with 16 additions and 3 deletions

View File

@@ -66,7 +66,7 @@ func (c *rootCmd) Register() {
{"ldap-insecure", false, "Skip certificate verification for the LDAP server."}, {"ldap-insecure", false, "Skip certificate verification for the LDAP server."},
{"ldap-search-filter", "(uid=%s)", "LDAP search filter for user lookup."}, {"ldap-search-filter", "(uid=%s)", "LDAP search filter for user lookup."},
{"resources-dir", "/data/resources", "Path to a directory containing custom resources (e.g. background image)."}, {"resources-dir", "/data/resources", "Path to a directory containing custom resources (e.g. background image)."},
{"database-path", "/data/tinyauth.db", "Path to the Sqlite database file."}, {"database-path", "/data/tinyauth.db", "Path to the Sqlite database file. Directory will be created if it doesn't exist."},
{"trusted-proxies", "", "Comma separated list of trusted proxies (IP addresses or CIDRs) for correct client IP detection."}, {"trusted-proxies", "", "Comma separated list of trusted proxies (IP addresses or CIDRs) for correct client IP detection."},
{"disable-analytics", false, "Disable anonymous version collection."}, {"disable-analytics", false, "Disable anonymous version collection."},
{"disable-resources", false, "Disable the resources server."}, {"disable-resources", false, "Disable the resources server."},

View File

@@ -37,7 +37,7 @@ type Config struct {
LdapInsecure bool `mapstructure:"ldap-insecure"` LdapInsecure bool `mapstructure:"ldap-insecure"`
LdapSearchFilter string `mapstructure:"ldap-search-filter"` LdapSearchFilter string `mapstructure:"ldap-search-filter"`
ResourcesDir string `mapstructure:"resources-dir"` ResourcesDir string `mapstructure:"resources-dir"`
DatabasePath string `mapstructure:"database-path" validate:"required"` DatabasePath string `mapstructure:"database-path"`
TrustedProxies string `mapstructure:"trusted-proxies"` TrustedProxies string `mapstructure:"trusted-proxies"`
DisableAnalytics bool `mapstructure:"disable-analytics"` DisableAnalytics bool `mapstructure:"disable-analytics"`
DisableResources bool `mapstructure:"disable-resources"` DisableResources bool `mapstructure:"disable-resources"`

View File

@@ -2,6 +2,9 @@ package service
import ( import (
"database/sql" "database/sql"
"fmt"
"os"
"path/filepath"
"tinyauth/internal/assets" "tinyauth/internal/assets"
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
@@ -27,7 +30,17 @@ func NewDatabaseService(config DatabaseServiceConfig) *DatabaseService {
} }
func (ds *DatabaseService) Init() error { func (ds *DatabaseService) Init() error {
gormDB, err := gorm.Open(sqlite.Open(ds.config.DatabasePath), &gorm.Config{}) dbPath := ds.config.DatabasePath
if dbPath == "" {
dbPath = "/data/tinyauth.db"
}
dir := filepath.Dir(dbPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create database directory %s: %w", dir, err)
}
gormDB, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
if err != nil { if err != nil {
return err return err