fix: load env once

This commit is contained in:
lilong.129
2025-04-21 22:50:00 +08:00
parent 70a8ee01f7
commit 9418c651f4
3 changed files with 8 additions and 5 deletions
+2 -2
View File
@@ -31,11 +31,11 @@ type Config struct {
var ( var (
globalConfig *Config globalConfig *Config
once sync.Once getConfigOnce sync.Once
) )
func GetConfig() *Config { func GetConfig() *Config {
once.Do(func() { getConfigOnce.Do(func() {
cfg := &Config{ cfg := &Config{
StartTime: time.Now(), StartTime: time.Now(),
} }
+4 -1
View File
@@ -5,15 +5,18 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
var loadEnvOnce sync.Once
// 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() (err error) { func LoadEnv() (err error) {
once.Do(func() { loadEnvOnce.Do(func() {
// get current working directory // get current working directory
var cwd string var cwd string
cwd, err = os.Getwd() cwd, err = os.Getwd()
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2504212133 v5.0.0-beta-2504212250