From 770687a2f6a022e493ced28b49768098cadf5ffa Mon Sep 17 00:00:00 2001 From: suyiiyii <suyiiyii@gmail.com> Date: Mon, 20 Jan 2025 23:21:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E5=B5=8C=E5=85=A5=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E5=88=B0=E5=8F=AF=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 go:embed 导入配置文件,避免在运行时读取文件系统中的配置 - 移除了文件读取相关代码,简化了配置加载过程 - 这种方法提高了应用程序的可移植性和安全性 --- app/auth/conf/conf.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/auth/conf/conf.go b/app/auth/conf/conf.go index 1f929e8..85cceb2 100644 --- a/app/auth/conf/conf.go +++ b/app/auth/conf/conf.go @@ -1,9 +1,8 @@ package conf import ( - "io/ioutil" + _ "embed" "os" - "path/filepath" "sync" "github.com/cloudwego/kitex/pkg/klog" @@ -12,6 +11,8 @@ import ( "gopkg.in/yaml.v2" ) +//go:embed dev/conf.yaml +var configFile []byte var ( conf *Config once sync.Once @@ -59,14 +60,15 @@ func GetConf() *Config { } func initConf() { - prefix := "conf" - confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) - content, err := ioutil.ReadFile(confFileRelPath) - if err != nil { - panic(err) - } + //prefix := "conf" + //confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) + //content, err := ioutil.ReadFile(confFileRelPath) + content := configFile + //if err != nil { + // panic(err) + //} conf = new(Config) - err = yaml.Unmarshal(content, conf) + err := yaml.Unmarshal(content, conf) if err != nil { klog.Error("parse yaml error - %v", err) panic(err)