Skip to content

Commit

Permalink
fix path bug
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 8, 2021
1 parent 9c4cf8a commit 271fdd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
28 changes: 23 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ type ServiceConfig struct {

// DatabaseConfig 数据库配置
type DatabaseConfig struct {
Debug bool `json:"debug,omitempty" env:"XIANG_DB_DEBUG" envDefault:"false"` // DEBUG 开关
Driver string `json:"driver,omitempty" env:"XIANG_DB_DRIVER" envDefault:"sqlite3"` // 数据库驱动 ( sqlite3, mysql, postgres)
Primary []string `json:"primary,omitempty" env:"XIANG_DB_PRIMARY" envSeparator:"|" envDefault:"file:xiang.db?cache=shared&mode=memory"` // 主库连接DSN
Secondary []string `json:"secondary,omitempty" env:"XIANG_DB_SECONDARY" envSeparator:"|"` // 从库连接DSN
AESKey string `json:"aeskey,omitempty" env:"XIANG_DB_AESKEY"` // 加密存储KEY
Debug bool `json:"debug,omitempty" env:"XIANG_DB_DEBUG" envDefault:"false"` // DEBUG 开关
Driver string `json:"driver,omitempty" env:"XIANG_DB_DRIVER" envDefault:"sqlite3"` // 数据库驱动 ( sqlite3, mysql, postgres)
Primary []string `json:"primary,omitempty" env:"XIANG_DB_PRIMARY" envSeparator:"|" envDefault:"file:./db/xiang.db"` // 主库连接DSN
Secondary []string `json:"secondary,omitempty" env:"XIANG_DB_SECONDARY" envSeparator:"|"` // 从库连接DSN
AESKey string `json:"aeskey,omitempty" env:"XIANG_DB_AESKEY"` // 加密存储KEY
}

// StorageConfig 存储配置
Expand Down Expand Up @@ -129,6 +129,16 @@ func NewConfigFrom(input io.Reader) Config {

// SetDefaults 设定默认值
func (cfg *Config) SetDefaults() {

pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

if cfg.Root == "fs://." || cfg.Root == "." {
cfg.Root = pwd
}

if cfg.RootAPI == "" {
cfg.RootAPI = cfg.Root + "/apis"
}
Expand Down Expand Up @@ -164,6 +174,14 @@ func (cfg *Config) SetDefaults() {
cfg.RootUI = cfg.Root + "/ui"
}

if len(cfg.Database.Primary) > 0 {
if strings.HasPrefix(cfg.Database.Primary[0], "file:.") {
cfg.Database.Primary[0] = strings.ReplaceAll(cfg.Database.Primary[0], "file:.", pwd)
} else if strings.HasPrefix(cfg.Database.Primary[0], "file:/") {
cfg.Database.Primary[0] = strings.ReplaceAll(cfg.Database.Primary[0], "file:.", cfg.RootDB)
}
}

// 过滤数据
cfg.RootDB = strings.TrimPrefix(cfg.RootDB, "fs://")
cfg.RootDB = strings.TrimPrefix(cfg.RootDB, "file://")
Expand Down
7 changes: 7 additions & 0 deletions global/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func AppInit(cfg config.Config) {
log.Panicf("创建目录失败(%s) %s", cfg.RootDB, err)
}
}

if _, err := os.Stat(cfg.RootTable); os.IsNotExist(err) {
err := os.MkdirAll(cfg.RootTable, os.ModePerm)
if err != nil {
log.Panicf("创建目录失败(%s) %s", cfg.RootTable, err)
}
}
}

// LoadAppInfo 读取应用信息
Expand Down

0 comments on commit 271fdd4

Please sign in to comment.