mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
change: load env once
This commit is contained in:
@@ -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 @@
|
|||||||
v5.0.0-beta-2503221320
|
v5.0.0-beta-2503221523
|
||||||
|
|||||||
+32
-31
@@ -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,47 +33,46 @@ 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() {
|
||||||
// get current working directory
|
once.Do(func() {
|
||||||
cwd, err := os.Getwd()
|
// get current working directory
|
||||||
if err != nil {
|
cwd, err := os.Getwd()
|
||||||
return fmt.Errorf("failed to get current working directory: %v", err)
|
if err != nil {
|
||||||
}
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
// locate .env file from current working directory upward recursively
|
// locate .env file from current working directory upward recursively
|
||||||
envPath := cwd
|
envPath := cwd
|
||||||
for {
|
for {
|
||||||
envFile := filepath.Join(envPath, ".env")
|
envFile := filepath.Join(envPath, ".env")
|
||||||
if _, err := os.Stat(envFile); err == nil {
|
if _, err := os.Stat(envFile); err == nil {
|
||||||
// found .env file
|
// found .env file
|
||||||
// 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("overload env success")
|
||||||
}
|
}
|
||||||
log.Info().Str("path", envFile).Msg("load 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),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user