Enhance configuration path resolution in LoadWithRoot function

- Update the LoadWithRoot function in config.go to ensure DataRoot is set correctly when it is empty, and resolve relative database paths for SQLite3 driver based on the Root directory.
- Improve handling of non-absolute paths for primary and secondary database connections, enhancing overall configuration management.
This commit is contained in:
Max 2026-02-21 16:11:26 +08:00
parent 2d86ee5b6e
commit 88c55b39c0

View file

@ -138,8 +138,20 @@ func LoadWithRoot(root string) Config {
// DataRoot
if cfg.DataRoot == "" {
cfg.DataRoot = filepath.Join(cfg.Root, "data")
if !filepath.IsAbs(cfg.DataRoot) {
cfg.DataRoot, _ = filepath.Abs(cfg.DataRoot)
}
if !filepath.IsAbs(cfg.DataRoot) {
cfg.DataRoot = filepath.Join(cfg.Root, cfg.DataRoot)
}
// Resolve DB relative paths based on Root
for i, dsn := range cfg.DB.Primary {
if !filepath.IsAbs(dsn) && (cfg.DB.Driver == "sqlite3" || cfg.DB.Driver == "") {
cfg.DB.Primary[i] = filepath.Join(cfg.Root, dsn)
}
}
for i, dsn := range cfg.DB.Secondary {
if !filepath.IsAbs(dsn) && (cfg.DB.Driver == "sqlite3" || cfg.DB.Driver == "") {
cfg.DB.Secondary[i] = filepath.Join(cfg.Root, dsn)
}
}