From a86156659d91a8bceded801b1160dc444f4233ad Mon Sep 17 00:00:00 2001 From: timerzz Date: Mon, 26 Aug 2024 16:36:40 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/database/init.go | 1 + pkg/database/options.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/database/init.go b/pkg/database/init.go index f96fa9f..915f279 100644 --- a/pkg/database/init.go +++ b/pkg/database/init.go @@ -2,6 +2,7 @@ package database import ( "fmt" + "gorm.io/driver/postgres" "gorm.io/gorm" ) diff --git a/pkg/database/options.go b/pkg/database/options.go index bc13ef3..7541a40 100644 --- a/pkg/database/options.go +++ b/pkg/database/options.go @@ -1,5 +1,11 @@ package database +import ( + "os" + + "gopkg.in/yaml.v3" +) + type DBOption struct { Host string `yaml:"host"` User string `yaml:"user"` @@ -7,3 +13,18 @@ type DBOption struct { Port string `yaml:"port"` 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) +}