config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/eryajf/chatgpt-dingtalk/pkg/logger"
  12. "gopkg.in/yaml.v2"
  13. )
  14. // Configuration 项目配置
  15. type Configuration struct {
  16. // 日志级别,info或者debug
  17. LogLevel string `yaml:"log_level"`
  18. // gtp apikey
  19. ApiKey string `yaml:"api_key"`
  20. // 请求的 URL 地址
  21. BaseURL string `yaml:"base_url"`
  22. // 使用模型
  23. Model string `yaml:"model"`
  24. // 会话超时时间
  25. SessionTimeout time.Duration `yaml:"session_timeout"`
  26. // 默认对话模式
  27. DefaultMode string `yaml:"default_mode"`
  28. // 代理地址
  29. HttpProxy string `yaml:"http_proxy"`
  30. // 用户单日最大请求次数
  31. MaxRequest int `yaml:"max_request"`
  32. // 指定服务启动端口,默认为 8090
  33. Port string `yaml:"port"`
  34. // 指定服务的地址,就是钉钉机器人配置的回调地址,比如: http://chat.eryajf.net
  35. ServiceURL string `yaml:"service_url"`
  36. // 限定对话类型 0:不限 1:单聊 2:群聊
  37. ChatType string `yaml:"chat_type"`
  38. // 哪些群组可以进行对话
  39. AllowGroups []string `yaml:"allow_groups"`
  40. // 哪些用户可以进行对话
  41. AllowUsers []string `yaml:"allow_users"`
  42. // 指定哪些人为此系统的管理员,必须指定,否则所有人都是
  43. AdminUsers []string `yaml:"admin_users"`
  44. // 钉钉机器人在应用信息中的AppSecret,为了校验回调的请求是否合法
  45. AppSecret string `yaml:"app_secret"`
  46. }
  47. var config *Configuration
  48. var once sync.Once
  49. // LoadConfig 加载配置
  50. func LoadConfig() *Configuration {
  51. once.Do(func() {
  52. // 从文件中读取
  53. config = &Configuration{}
  54. data, err := ioutil.ReadFile("config.yml")
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. err = yaml.Unmarshal(data, &config)
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. // 如果环境变量有配置,读取环境变量
  63. logLevel := os.Getenv("LOG_LEVEL")
  64. if logLevel != "" {
  65. config.LogLevel = logLevel
  66. }
  67. apiKey := os.Getenv("APIKEY")
  68. if apiKey != "" {
  69. config.ApiKey = apiKey
  70. }
  71. baseURL := os.Getenv("BASE_URL")
  72. if baseURL != "" {
  73. config.BaseURL = baseURL
  74. }
  75. model := os.Getenv("MODEL")
  76. if model != "" {
  77. config.Model = model
  78. }
  79. sessionTimeout := os.Getenv("SESSION_TIMEOUT")
  80. if sessionTimeout != "" {
  81. duration, err := strconv.ParseInt(sessionTimeout, 10, 64)
  82. if err != nil {
  83. logger.Fatal(fmt.Sprintf("config session timeout err: %v ,get is %v", err, sessionTimeout))
  84. return
  85. }
  86. config.SessionTimeout = time.Duration(duration) * time.Second
  87. } else {
  88. config.SessionTimeout = time.Duration(config.SessionTimeout) * time.Second
  89. }
  90. defaultMode := os.Getenv("DEFAULT_MODE")
  91. if defaultMode != "" {
  92. config.DefaultMode = defaultMode
  93. }
  94. httpProxy := os.Getenv("HTTP_PROXY")
  95. if httpProxy != "" {
  96. config.HttpProxy = httpProxy
  97. }
  98. maxRequest := os.Getenv("MAX_REQUEST")
  99. if maxRequest != "" {
  100. newMR, _ := strconv.Atoi(maxRequest)
  101. config.MaxRequest = newMR
  102. }
  103. port := os.Getenv("PORT")
  104. if port != "" {
  105. config.Port = port
  106. }
  107. serviceURL := os.Getenv("SERVICE_URL")
  108. if serviceURL != "" {
  109. config.ServiceURL = serviceURL
  110. }
  111. chatType := os.Getenv("CHAT_TYPE")
  112. if chatType != "" {
  113. config.ChatType = chatType
  114. }
  115. allowGroup := os.Getenv("ALLOW_GROUPS")
  116. if allowGroup != "" {
  117. config.AllowGroups = strings.Split(allowGroup, ",")
  118. }
  119. allowUsers := os.Getenv("ALLOW_USERS")
  120. if allowUsers != "" {
  121. config.AllowUsers = strings.Split(allowUsers, ",")
  122. }
  123. adminUsers := os.Getenv("ADMIN_USERS")
  124. if adminUsers != "" {
  125. config.AdminUsers = strings.Split(adminUsers, ",")
  126. }
  127. appSecret := os.Getenv("APP_SECRET")
  128. if appSecret != "" {
  129. config.AppSecret = appSecret
  130. }
  131. })
  132. // 一些默认值
  133. if config.LogLevel == "" {
  134. config.LogLevel = "info"
  135. }
  136. if config.Model == "" {
  137. config.Model = "gpt-3.5-turbo"
  138. }
  139. if config.DefaultMode == "" {
  140. config.DefaultMode = "单聊"
  141. }
  142. if config.Port == "" {
  143. config.Port = "8090"
  144. }
  145. if config.ChatType == "" {
  146. config.ChatType = "0"
  147. }
  148. if config.ApiKey == "" {
  149. logger.Fatal("config err: api key required")
  150. }
  151. if config.ServiceURL == "" {
  152. logger.Fatal("config err: service url required")
  153. }
  154. return config
  155. }