change: load env once

This commit is contained in:
lilong.129
2025-03-22 15:23:39 +08:00
parent 8125191ffb
commit 148d70accf
3 changed files with 34 additions and 32 deletions
+1
View File
@@ -16,6 +16,7 @@ bump: ## bump hrp version, e.g. make bump version=4.0.0
.PHONY: build .PHONY: build
build: ## build hrp cli tool build: ## build hrp cli tool
@echo "[info] build hrp cli tool" @echo "[info] build hrp cli tool"
go mod tidy
go build -ldflags "\ go build -ldflags "\
-s -w \ -s -w \
-X 'github.com/httprunner/httprunner/v5/internal/version.GitCommit=$(shell git rev-parse HEAD)' \ -X 'github.com/httprunner/httprunner/v5/internal/version.GitCommit=$(shell git rev-parse HEAD)' \
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2503221320 v5.0.0-beta-2503221523
+13 -12
View File
@@ -7,6 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync"
"time" "time"
"github.com/cloudwego/eino-ext/components/model/openai" "github.com/cloudwego/eino-ext/components/model/openai"
@@ -32,20 +33,16 @@ const (
EnvOpenAIInitConfigJSON = "OPENAI_INIT_CONFIG_JSON" EnvOpenAIInitConfigJSON = "OPENAI_INIT_CONFIG_JSON"
) )
func init() { var once sync.Once
err := loadEnv()
if err != nil {
log.Warn().Err(err).Msg("load .env file failed")
}
}
// loadEnv loads environment variables from .env file // loadEnv loads environment variables from .env file
// it will search for .env file from current working directory upward recursively // it will search for .env file from current working directory upward recursively
func loadEnv() error { func loadEnv() {
once.Do(func() {
// get current working directory // get current working directory
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
return fmt.Errorf("failed to get current working directory: %v", err) panic(err)
} }
// locate .env file from current working directory upward recursively // locate .env file from current working directory upward recursively
@@ -57,22 +54,25 @@ func loadEnv() error {
// override existing env variables // override existing env variables
err = godotenv.Overload(envFile) err = godotenv.Overload(envFile)
if err != nil { if err != nil {
return fmt.Errorf("failed to load env file: %v", err) log.Fatal().Err(err).
Str("path", envFile).Msg("overload env file failed")
} }
log.Info().Str("path", envFile).Msg("load env success") log.Info().Str("path", envFile).Msg("overload env success")
return nil
} }
// reached root directory // reached root directory
parent := filepath.Dir(envPath) parent := filepath.Dir(envPath)
if parent == envPath { if parent == envPath {
return fmt.Errorf("no .env file found from current directory to root") log.Info().Msg("no .env file found from current directory to root")
return
} }
envPath = parent envPath = parent
} }
})
} }
func checkEnvLLM() error { func checkEnvLLM() error {
loadEnv()
openaiBaseURL := os.Getenv("OPENAI_BASE_URL") openaiBaseURL := os.Getenv("OPENAI_BASE_URL")
if openaiBaseURL == "" { if openaiBaseURL == "" {
return errors.Wrap(code.LLMEnvMissedError, "OPENAI_BASE_URL missed") return errors.Wrap(code.LLMEnvMissedError, "OPENAI_BASE_URL missed")
@@ -156,6 +156,7 @@ func (c *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// GetModelConfig get OpenAI config // GetModelConfig get OpenAI config
func GetModelConfig() (*openai.ChatModelConfig, error) { func GetModelConfig() (*openai.ChatModelConfig, error) {
loadEnv()
envConfig := &OpenAIInitConfig{ envConfig := &OpenAIInitConfig{
Headers: make(map[string]string), Headers: make(map[string]string),
} }