config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package config
  2. import (
  3. "os"
  4. "strconv"
  5. "time"
  6. "github.com/spf13/viper"
  7. )
  8. type Config struct {
  9. Server ServerConfig `mapstructure:"server"`
  10. Database DatabaseConfig `mapstructure:"database"`
  11. Speed SpeedConfig `mapstructure:"speed"`
  12. Log LogConfig `mapstructure:"log"`
  13. }
  14. type ServerConfig struct {
  15. Port int `mapstructure:"port"`
  16. Host string `mapstructure:"host"`
  17. }
  18. type DatabaseConfig struct {
  19. Host string `mapstructure:"host"`
  20. Port int `mapstructure:"port"`
  21. Username string `mapstructure:"username"`
  22. Password string `mapstructure:"password"`
  23. Database string `mapstructure:"database"`
  24. }
  25. type SpeedConfig struct {
  26. TestURLs []string `mapstructure:"test_urls"`
  27. Timeout time.Duration `mapstructure:"timeout"`
  28. Concurrency int `mapstructure:"concurrency"`
  29. Interval time.Duration `mapstructure:"interval"`
  30. TestOnStart bool `mapstructure:"test_on_start"`
  31. }
  32. type LogConfig struct {
  33. Level string `mapstructure:"level"`
  34. File string `mapstructure:"file"`
  35. }
  36. func Load() (*Config, error) {
  37. // 设置默认值
  38. setDefaults()
  39. // 读取环境变量
  40. viper.AutomaticEnv()
  41. // 读取配置文件
  42. viper.SetConfigName("config")
  43. viper.SetConfigType("yaml")
  44. viper.AddConfigPath(".")
  45. viper.AddConfigPath("./config")
  46. if err := viper.ReadInConfig(); err != nil {
  47. // 如果配置文件不存在,使用默认值
  48. if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
  49. return nil, err
  50. }
  51. }
  52. var config Config
  53. if err := viper.Unmarshal(&config); err != nil {
  54. return nil, err
  55. }
  56. return &config, nil
  57. }
  58. func setDefaults() {
  59. // 服务器配置
  60. viper.SetDefault("server.port", 3000)
  61. viper.SetDefault("server.host", "0.0.0.0")
  62. // 数据库配置
  63. viper.SetDefault("database.host", "localhost")
  64. viper.SetDefault("database.port", 3306)
  65. viper.SetDefault("database.username", "root")
  66. viper.SetDefault("database.password", "")
  67. viper.SetDefault("database.database", "clash_speed_test")
  68. // 测速配置
  69. viper.SetDefault("speed.test_urls", []string{
  70. "https://www.google.com",
  71. "https://www.youtube.com",
  72. "https://www.github.com",
  73. })
  74. viper.SetDefault("speed.timeout", 10*time.Second)
  75. viper.SetDefault("speed.concurrency", 5)
  76. viper.SetDefault("speed.interval", 5*time.Minute)
  77. viper.SetDefault("speed.test_on_start", true)
  78. // 日志配置
  79. viper.SetDefault("log.level", "info")
  80. viper.SetDefault("log.file", "")
  81. // 环境变量覆盖
  82. if port := os.Getenv("PORT"); port != "" {
  83. if p, err := strconv.Atoi(port); err == nil {
  84. viper.Set("server.port", p)
  85. }
  86. }
  87. if dbHost := os.Getenv("DB_HOST"); dbHost != "" {
  88. viper.Set("database.host", dbHost)
  89. }
  90. if dbPort := os.Getenv("DB_PORT"); dbPort != "" {
  91. if p, err := strconv.Atoi(dbPort); err == nil {
  92. viper.Set("database.port", p)
  93. }
  94. }
  95. if dbUser := os.Getenv("DB_USER"); dbUser != "" {
  96. viper.Set("database.username", dbUser)
  97. }
  98. if dbPass := os.Getenv("DB_PASS"); dbPass != "" {
  99. viper.Set("database.password", dbPass)
  100. }
  101. if dbName := os.Getenv("DB_NAME"); dbName != "" {
  102. viper.Set("database.database", dbName)
  103. }
  104. if interval := os.Getenv("SPEED_TEST_INTERVAL"); interval != "" {
  105. if d, err := time.ParseDuration(interval); err == nil {
  106. viper.Set("speed.interval", d)
  107. }
  108. }
  109. if timeout := os.Getenv("SPEED_TEST_TIMEOUT"); timeout != "" {
  110. if d, err := time.ParseDuration(timeout); err == nil {
  111. viper.Set("speed.timeout", d)
  112. }
  113. }
  114. }