feat(auth): 嵌入配置文件到可执行文件

- 使用 go:embed 导入配置文件,避免在运行时读取文件系统中的配置
- 移除了文件读取相关代码,简化了配置加载过程
- 这种方法提高了应用程序的可移植性和安全性
This commit is contained in:
suyiiyii 2025-01-20 23:21:21 +08:00
parent bf0daf8742
commit 770687a2f6
Signed by: suyiiyii
GPG Key ID: 044704CB29B8AD85

View File

@ -1,9 +1,8 @@
package conf package conf
import ( import (
"io/ioutil" _ "embed"
"os" "os"
"path/filepath"
"sync" "sync"
"github.com/cloudwego/kitex/pkg/klog" "github.com/cloudwego/kitex/pkg/klog"
@ -12,6 +11,8 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
//go:embed dev/conf.yaml
var configFile []byte
var ( var (
conf *Config conf *Config
once sync.Once once sync.Once
@ -59,14 +60,15 @@ func GetConf() *Config {
} }
func initConf() { func initConf() {
prefix := "conf" //prefix := "conf"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) //confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := ioutil.ReadFile(confFileRelPath) //content, err := ioutil.ReadFile(confFileRelPath)
if err != nil { content := configFile
panic(err) //if err != nil {
} // panic(err)
//}
conf = new(Config) conf = new(Config)
err = yaml.Unmarshal(content, conf) err := yaml.Unmarshal(content, conf)
if err != nil { if err != nil {
klog.Error("parse yaml error - %v", err) klog.Error("parse yaml error - %v", err)
panic(err) panic(err)