58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
// Configuration 项目配置
|
|
type Configuration struct {
|
|
// gpt apikey
|
|
ApiKey string `json:"api_key"`
|
|
// 自动通过好友
|
|
AutoPass bool `json:"auto_pass"`
|
|
|
|
// bing 聊天接口
|
|
BingChatUrl string `json:"bing_chat_url"`
|
|
}
|
|
|
|
var config *Configuration
|
|
var once sync.Once
|
|
|
|
// LoadConfig 加载配置
|
|
func LoadConfig() *Configuration {
|
|
once.Do(func() {
|
|
// 从文件中读取
|
|
config = &Configuration{}
|
|
f, err := os.Open("config.json")
|
|
if err != nil {
|
|
log.Fatalf("open config err: %v", err)
|
|
return
|
|
}
|
|
defer f.Close()
|
|
encoder := json.NewDecoder(f)
|
|
err = encoder.Decode(config)
|
|
if err != nil {
|
|
log.Fatalf("decode config err: %v", err)
|
|
return
|
|
}
|
|
|
|
// 如果环境变量有配置,读取环境变量
|
|
ApiKey := os.Getenv("ApiKey")
|
|
AutoPass := os.Getenv("AutoPass")
|
|
BingChatUrl := os.Getenv("BingChatUrl")
|
|
if ApiKey != "" {
|
|
config.ApiKey = ApiKey
|
|
}
|
|
if AutoPass == "true" {
|
|
config.AutoPass = true
|
|
}
|
|
if BingChatUrl != "" {
|
|
config.BingChatUrl = BingChatUrl
|
|
}
|
|
})
|
|
return config
|
|
}
|