feat 添加读取数据库配置

This commit is contained in:
timerzz 2024-08-26 16:36:40 +08:00
parent 27f10ca798
commit a86156659d
2 changed files with 22 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package database
import ( import (
"fmt" "fmt"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
) )

View File

@ -1,5 +1,11 @@
package database package database
import (
"os"
"gopkg.in/yaml.v3"
)
type DBOption struct { type DBOption struct {
Host string `yaml:"host"` Host string `yaml:"host"`
User string `yaml:"user"` User string `yaml:"user"`
@ -7,3 +13,18 @@ type DBOption struct {
Port string `yaml:"port"` Port string `yaml:"port"`
DBName string `yaml:"dbname"` DBName string `yaml:"dbname"`
} }
const (
// 默认的数据库配置文件路径
DefaultConfigPath = "/cfg/db.yaml"
)
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)
}