This commit is contained in:
coward
2024-03-05 16:59:37 +08:00
commit 0e20f5068e
13 changed files with 445 additions and 0 deletions

9
config/config.go Normal file
View File

@@ -0,0 +1,9 @@
package config
var Config *config
type config struct {
Http *http `yaml:"http"`
Database *database `yaml:"database"`
Redis *redis `yaml:"redis"`
}

25
config/databse.go Normal file
View File

@@ -0,0 +1,25 @@
package config
import "fmt"
type database struct {
Driver string `yaml:"driver"`
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Db string `yaml:"db"`
}
func (d database) GetDSN() string {
switch d.Driver {
case "mysql":
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", d.User, d.Password, d.Host, d.Port, d.Db)
case "pgsql":
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", d.Host, d.User, d.Password, d.Db, d.Port)
case "sqlite":
return fmt.Sprintf("%s.db", d.Db)
}
return ""
}

5
config/http.go Normal file
View File

@@ -0,0 +1,5 @@
package config
type http struct {
Port uint `yaml:"port"`
}

8
config/redis.go Normal file
View File

@@ -0,0 +1,8 @@
package config
type redis struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Password string `yaml:"password"`
Db int `yaml:"db"`
}