Skip to content

Commit

Permalink
feat: add AppBaseSecretValue and AppConfigBody configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Nov 13, 2024
1 parent cd9c61b commit 7dceac9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
22 changes: 15 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ func LoadConfig() error {

// 从主配置文件读取配置
func readConfig() (*MainConf, error) {
body, err := os.ReadFile(ConfigFile)
if err != nil {
return nil, err
body := []byte(AppConfigBody)
if len(body) == 0 {
var err error
body, err = os.ReadFile(ConfigFile)
if err != nil {
return nil, err
}
}

cfg := new(MainConf)
Expand Down Expand Up @@ -192,10 +196,14 @@ func readConfig() (*MainConf, error) {
}

func parseSYSConfig(cfg *MainConf) error {
// 基础密钥: 由程序固化的密钥解密环境变量得到, 其他加密变量都使用基础密码加密
cfg.SYSConf.BaseSecretValue = xcrypto.GetenvDecrypt(BaseSecretEnvName, BaseSecretKeyValue)
if cfg.SYSConf.BaseSecretValue == "" {
return fmt.Errorf("%s cannot be empty", BaseSecretEnvName)
if AppBaseSecretValue != "" {
cfg.SYSConf.BaseSecretValue = AppBaseSecretValue
} else {
// 基础密钥: 由程序固化的密钥解密环境变量得到, 其他加密变量都使用基础密码加密
cfg.SYSConf.BaseSecretValue = xcrypto.GetenvDecrypt(BaseSecretEnvName, BaseSecretKeyValue)
if cfg.SYSConf.BaseSecretValue == "" {
return fmt.Errorf("%s cannot be empty", BaseSecretEnvName)
}
}
// 赋值到全局变量
BaseSecretValue = cfg.SYSConf.BaseSecretValue
Expand Down
7 changes: 6 additions & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var (
BinNameEnvName = "LOCAL_BIN_NAME"
AppNameEnvName = "LOCAL_APP_NAME"
DebNameEnvName = "LOCAL_DEB_NAME"

// AppBaseSecretValue APP 设置的基础密钥值(可选, 优先使用)
AppBaseSecretValue string
// AppConfigBody APP 设置的固定配置 JSON 字符串, 替代配置文件 (可选, 优先使用)
AppConfigBody string
)

var (
Expand Down Expand Up @@ -75,7 +80,7 @@ var (
LogPostBatchNum = 2000
LogPostBatchBytes = 2 << 20

// BaseSecretValue 项目基础密钥值, 与 config.Config().SYSConf.BaseSecretValue 相同
// BaseSecretValue 项目基础密钥值(从环境变量解码), 与 config.Config().SYSConf.BaseSecretValue 相同
BaseSecretValue string
// BaseSecretEnvName 项目基础密钥 (环境变量名)
BaseSecretEnvName = "BASE_SECRET_KEY"
Expand Down

0 comments on commit 7dceac9

Please sign in to comment.