feat(auth): 嵌入配置文件到可执行文件
- 使用 go:embed 导入配置文件,避免在运行时读取文件系统中的配置 - 移除了文件读取相关代码,简化了配置加载过程 - 这种方法提高了应用程序的可移植性和安全性
This commit is contained in:
parent
bf0daf8742
commit
770687a2f6
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user