config.go 3.6 KB

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