feat(osi): 打通 Go 侧真实请求验收(T-005)

This commit is contained in:
ila
2026-07-06 23:03:14 +08:00
parent 2a77a4dbc1
commit def95efb1c
12 changed files with 411 additions and 54 deletions
+32 -1
View File
@@ -1,6 +1,10 @@
package config
import "github.com/spf13/viper"
import (
"strings"
"github.com/spf13/viper"
)
type Config struct {
OSI OSIConfig `mapstructure:"osi"`
@@ -38,6 +42,7 @@ type RedisConfig struct {
func Load(path string) (Config, error) {
v := viper.New()
setDefaults(v)
bindEnv(v)
if path != "" {
v.SetConfigFile(path)
@@ -66,3 +71,29 @@ func setDefaults(v *viper.Viper) {
v.SetDefault("circuit_fail_threshold", 5)
v.SetDefault("circuit_sleep_sec", 60)
}
func bindEnv(v *viper.Viper) {
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
for _, key := range []string{
"osi.base_url",
"osi.org_code",
"osi.user_name",
"osi.ask",
"osi.device_sn",
"osi.operate_user",
"osi.timeout_sec",
"osi.socks5_proxy",
"phis.base_url",
"phis.token",
"phis.region_code",
"phis.poll_interval_sec",
"redis.addr",
"redis.db",
"report_log_file_dir",
"retry_max",
"circuit_fail_threshold",
"circuit_sleep_sec",
} {
_ = v.BindEnv(key)
}
}
+39
View File
@@ -0,0 +1,39 @@
package config
import "testing"
func TestLoadReadsOSIEnvironmentOverrides(t *testing.T) {
t.Setenv("OSI_BASE_URL", "http://env-osi-host")
t.Setenv("OSI_ORG_CODE", "env-org")
t.Setenv("OSI_USER_NAME", "env-user")
t.Setenv("OSI_ASK", "env-ask")
t.Setenv("OSI_DEVICE_SN", "env-device")
t.Setenv("OSI_OPERATE_USER", "env-operate-user")
t.Setenv("OSI_SOCKS5_PROXY", "env-proxy:11002")
cfg, err := Load("")
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.OSI.BaseURL != "http://env-osi-host" {
t.Fatalf("BaseURL = %q", cfg.OSI.BaseURL)
}
if cfg.OSI.OrgCode != "env-org" {
t.Fatalf("OrgCode = %q", cfg.OSI.OrgCode)
}
if cfg.OSI.UserName != "env-user" {
t.Fatalf("UserName = %q", cfg.OSI.UserName)
}
if cfg.OSI.Ask != "env-ask" {
t.Fatalf("Ask = %q", cfg.OSI.Ask)
}
if cfg.OSI.DeviceSN != "env-device" {
t.Fatalf("DeviceSN = %q", cfg.OSI.DeviceSN)
}
if cfg.OSI.OperateUser != "env-operate-user" {
t.Fatalf("OperateUser = %q", cfg.OSI.OperateUser)
}
if cfg.OSI.Socks5Proxy != "env-proxy:11002" {
t.Fatalf("Socks5Proxy = %q", cfg.OSI.Socks5Proxy)
}
}