2024-05-13 19:58:26 +08:00
|
|
|
package database
|
|
|
|
|
2024-08-26 16:36:40 +08:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2024-05-13 19:58:26 +08:00
|
|
|
type DBOption struct {
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
User string `yaml:"user"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
Port string `yaml:"port"`
|
|
|
|
DBName string `yaml:"dbname"`
|
|
|
|
}
|
2024-08-26 16:36:40 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
// 默认的数据库配置文件路径
|
|
|
|
DefaultConfigPath = "/cfg/db.yaml"
|
2024-08-26 16:54:50 +08:00
|
|
|
// 数据库配置文件路径环境变量
|
|
|
|
ConfigPathEnvKey = "DB_CONFIG_PATH"
|
2024-08-26 16:36:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func LoadDBConfig(path string) (*DBOption, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var opt DBOption
|
|
|
|
return &opt, yaml.NewDecoder(f).Decode(&opt)
|
|
|
|
}
|