Improve web service related aspects

This commit is contained in:
2025-11-22 17:09:25 +08:00
parent 2a3ef3e52a
commit 27fc5c1fd6
7 changed files with 158 additions and 6 deletions

View File

@@ -0,0 +1,55 @@
package service
import (
"encoding/json"
"os"
"path/filepath"
"sync"
)
var (
config Config
configOnce sync.Once
configErr error
configPath string
)
// loadConfig initializes the config from file
func loadConfig() {
exePath, err := os.Executable()
configPath := os.Getenv("CONFIG_PATH")
if err != nil {
configErr = err
return
}
if configPath == "" {
exeDir := filepath.Dir(exePath)
configPath = filepath.Join(exeDir, "config.json")
}
data, err := os.ReadFile(configPath)
if err != nil {
configErr = err
return
}
err = json.Unmarshal(data, &config)
if err != nil {
configErr = err
return
}
}
// GetConfig returns the singleton instance of the config.
// It's safe for concurrent use and loads the config only once per process lifetime.
func GetConfig() (*Config, error) {
configOnce.Do(loadConfig)
return &config, configErr
}
// ConfigPath returns the path to the loaded config file (useful for logging or reload later if needed)
func ConfigPath() string {
// Make sure config is at least attempted to be loaded
configOnce.Do(loadConfig)
return configPath
}