config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. )
  6. type Config struct {
  7. Server ServerConfig
  8. MySQL MySQLConfig
  9. Redis RedisConfig
  10. Telegram TelegramConfig
  11. LLM LLMConfig
  12. Serper SerperConfig
  13. GitHub GitHubConfig
  14. }
  15. type ServerConfig struct {
  16. Port int
  17. }
  18. type MySQLConfig struct {
  19. Host string
  20. Port int
  21. User string
  22. Password string
  23. Database string
  24. }
  25. type RedisConfig struct {
  26. Host string
  27. Port int
  28. Password string
  29. DB int
  30. }
  31. type TelegramConfig struct {
  32. AppID int `mapstructure:"app_id"`
  33. AppHash string `mapstructure:"app_hash"`
  34. Accounts []TGAccount
  35. }
  36. type TGAccount struct {
  37. Phone string
  38. SessionFile string `mapstructure:"session_file"`
  39. }
  40. type LLMConfig struct {
  41. Provider string
  42. BaseURL string `mapstructure:"base_url"`
  43. APIKey string `mapstructure:"api_key"`
  44. Model string
  45. Timeout string
  46. }
  47. type SerperConfig struct {
  48. APIKey string `mapstructure:"api_key"`
  49. ResultsPerPage int `mapstructure:"results_per_page"`
  50. MaxPages int `mapstructure:"max_pages"`
  51. }
  52. type GitHubConfig struct {
  53. Token string
  54. }
  55. var global *Config
  56. func Load(path string) (*Config, error) {
  57. v := viper.New()
  58. v.SetConfigFile(path)
  59. v.SetConfigType("yaml")
  60. if err := v.ReadInConfig(); err != nil {
  61. return nil, fmt.Errorf("read config file: %w", err)
  62. }
  63. cfg := &Config{}
  64. if err := v.Unmarshal(cfg); err != nil {
  65. return nil, fmt.Errorf("unmarshal config: %w", err)
  66. }
  67. global = cfg
  68. return cfg, nil
  69. }
  70. func Get() *Config {
  71. return global
  72. }