config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "github.com/eryajf/chatgpt-dingtalk/pkg/logger"
  10. )
  11. // Configuration 项目配置
  12. type Configuration struct {
  13. // gtp apikey
  14. ApiKey string `json:"api_key"`
  15. // 请求的 URL 地址
  16. BaseURL string `json:"base_url"`
  17. // 使用模型
  18. Model string `json:"model"`
  19. // 会话超时时间
  20. SessionTimeout time.Duration `json:"session_timeout"`
  21. // 默认对话模式
  22. DefaultMode string `json:"default_mode"`
  23. // 代理地址
  24. HttpProxy string `json:"http_proxy"`
  25. // 用户单日最大请求次数
  26. MaxRequest int `json:"max_request"`
  27. // 指定服务启动端口,默认为 8090
  28. Port string `json:"port"`
  29. // 指定服务的地址,就是钉钉机器人配置的回调地址,比如: http://chat.eryajf.net
  30. ServiceURL string `json:"service_url"`
  31. }
  32. var config *Configuration
  33. var once sync.Once
  34. // LoadConfig 加载配置
  35. func LoadConfig() *Configuration {
  36. once.Do(func() {
  37. // 从文件中读取
  38. config = &Configuration{}
  39. f, err := os.Open("config.json")
  40. if err != nil {
  41. logger.Fatal(fmt.Errorf("open config err: %+v", err))
  42. return
  43. }
  44. defer f.Close()
  45. encoder := json.NewDecoder(f)
  46. err = encoder.Decode(config)
  47. if err != nil {
  48. logger.Warning(fmt.Errorf("decode config err: %v", err))
  49. return
  50. }
  51. // 如果环境变量有配置,读取环境变量
  52. apiKey := os.Getenv("APIKEY")
  53. baseURL := os.Getenv("BASE_URL")
  54. model := os.Getenv("MODEL")
  55. sessionTimeout := os.Getenv("SESSION_TIMEOUT")
  56. defaultMode := os.Getenv("DEFAULT_MODE")
  57. httpProxy := os.Getenv("HTTP_PROXY")
  58. maxRequest := os.Getenv("MAX_REQUEST")
  59. port := os.Getenv("PORT")
  60. serviceURL := os.Getenv("SERVICE_URL")
  61. if apiKey != "" {
  62. config.ApiKey = apiKey
  63. }
  64. if baseURL != "" {
  65. config.BaseURL = baseURL
  66. }
  67. if sessionTimeout != "" {
  68. duration, err := strconv.ParseInt(sessionTimeout, 10, 64)
  69. if err != nil {
  70. logger.Fatal(fmt.Sprintf("config session timeout err: %v ,get is %v", err, sessionTimeout))
  71. return
  72. }
  73. config.SessionTimeout = time.Duration(duration) * time.Second
  74. } else {
  75. config.SessionTimeout = time.Duration(config.SessionTimeout) * time.Second
  76. }
  77. if defaultMode != "" {
  78. config.DefaultMode = defaultMode
  79. }
  80. if httpProxy != "" {
  81. config.HttpProxy = httpProxy
  82. }
  83. if model != "" {
  84. config.Model = model
  85. }
  86. if maxRequest != "" {
  87. newMR, _ := strconv.Atoi(maxRequest)
  88. config.MaxRequest = newMR
  89. }
  90. if port != "" {
  91. config.Port = port
  92. }
  93. if serviceURL != "" {
  94. config.ServiceURL = serviceURL
  95. }
  96. })
  97. if config.Model == "" {
  98. config.DefaultMode = "gpt-3.5-turbo"
  99. }
  100. if config.DefaultMode == "" {
  101. config.DefaultMode = "单聊"
  102. }
  103. if config.Port == "" {
  104. config.Port = "8090"
  105. }
  106. if config.ApiKey == "" {
  107. logger.Fatal("config err: api key required")
  108. }
  109. if config.ServiceURL == "" {
  110. logger.Fatal("config err: service url required")
  111. }
  112. return config
  113. }