feat(t229): load ERP credentials from dotenv

This commit is contained in:
QiuSW
2026-07-29 10:25:56 +08:00
parent d75a7d8dc0
commit f0fbdbaddc
12 changed files with 396 additions and 26 deletions
+15 -1
View File
@@ -34,7 +34,7 @@ func main() {
}
func run() error {
cfg, err := config.Load(os.LookupEnv)
cfg, err := loadConfig(os.LookupEnv, config.DefaultERPEnvironmentFile)
if err != nil {
return err
}
@@ -93,6 +93,20 @@ func run() error {
}
}
func loadConfig(
lookup config.LookupEnvironment,
environmentFile string,
) (config.Config, error) {
effectiveLookup, err := config.WithERPEnvironmentFile(
environmentFile,
lookup,
)
if err != nil {
return config.Config{}, err
}
return config.Load(effectiveLookup)
}
func shutdownServer(
server *http.Server,
serverErrors <-chan error,
+35
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"
"time"
@@ -16,6 +17,40 @@ import (
"cmroubao/backend-api/internal/platform/migration"
)
func TestLoadConfigUsesERPEnvironmentFile(t *testing.T) {
environmentFile := filepath.Join(t.TempDir(), ".env")
err := os.WriteFile(environmentFile, []byte(
"CMROUBAO_SHUNYUNBAO_URL=https://erp.example.test\n"+
"CMROUBAO_SHUNYUNBAO_USERNAME=dotenv-user\n"+
"CMROUBAO_SHUNYUNBAO_PASSWORD=dotenv-password\n",
), 0o600)
if err != nil {
t.Fatalf("os.WriteFile() error = %v", err)
}
cfg, err := loadConfig(func(name string) (string, bool) {
if name == config.ShunyunbaoUsernameEnvironment {
return "process-user", true
}
return "", false
}, environmentFile)
if err != nil {
t.Fatalf("loadConfig() error = %v", err)
}
if cfg.ShunyunbaoURL != "https://erp.example.test" ||
cfg.ShunyunbaoUsername != "process-user" ||
cfg.ShunyunbaoPassword != "dotenv-password" {
t.Fatalf(
"ERP config = %#v",
struct {
URL string
Username string
Password string
}{cfg.ShunyunbaoURL, cfg.ShunyunbaoUsername, cfg.ShunyunbaoPassword},
)
}
}
func TestShutdownServerForceClosesAfterGracefulTimeout(t *testing.T) {
handlerStarted := make(chan struct{})
releaseHandler := make(chan struct{})