Skip to content

Commit e9307c5

Browse files
committed
feat: auto-create database directory if missing
1 parent 5f2ec02 commit e9307c5

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

cmd/root.go

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

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Config struct {
3737
LdapInsecure bool `mapstructure:"ldap-insecure"`
3838
LdapSearchFilter string `mapstructure:"ldap-search-filter"`
3939
ResourcesDir string `mapstructure:"resources-dir"`
40-
DatabasePath string `mapstructure:"database-path" validate:"required"`
40+
DatabasePath string `mapstructure:"database-path"`
4141
TrustedProxies string `mapstructure:"trusted-proxies"`
4242
DisableAnalytics bool `mapstructure:"disable-analytics"`
4343
DisableResources bool `mapstructure:"disable-resources"`

internal/service/database_service.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package service
22

33
import (
44
"database/sql"
5+
"fmt"
6+
"os"
7+
"path/filepath"
58
"tinyauth/internal/assets"
69

710
"github.com/glebarez/sqlite"
@@ -27,7 +30,17 @@ func NewDatabaseService(config DatabaseServiceConfig) *DatabaseService {
2730
}
2831

2932
func (ds *DatabaseService) Init() error {
30-
gormDB, err := gorm.Open(sqlite.Open(ds.config.DatabasePath), &gorm.Config{})
33+
dbPath := ds.config.DatabasePath
34+
if dbPath == "" {
35+
dbPath = "/data/tinyauth.db"
36+
}
37+
38+
dir := filepath.Dir(dbPath)
39+
if err := os.MkdirAll(dir, 0755); err != nil {
40+
return fmt.Errorf("failed to create database directory %s: %w", dir, err)
41+
}
42+
43+
gormDB, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
3144

3245
if err != nil {
3346
return err

0 commit comments

Comments
 (0)