common/pkg/database/options.go

33 lines
611 B
Go

package database
import (
"os"
"gopkg.in/yaml.v3"
)
type DBOption struct {
Host string `yaml:"host"`
User string `yaml:"user"`
Password string `yaml:"password"`
Port string `yaml:"port"`
DBName string `yaml:"dbname"`
}
const (
// 默认的数据库配置文件路径
DefaultConfigPath = "/cfg/db.yaml"
// 数据库配置文件路径环境变量
ConfigPathEnvKey = "DB_CONFIG_PATH"
)
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)
}