config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package config
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. "github.com/spf13/viper"
  8. )
  9. type Config struct {
  10. Server ServerConfig
  11. Database DatabaseConfig
  12. Stripe StripeConfig
  13. OVH OVHConfig
  14. Mailer MailerConfig
  15. Logging LoggingConfig
  16. JWT JWT
  17. }
  18. type ServerConfig struct {
  19. Address string
  20. Port int
  21. ReadTimeout time.Duration
  22. WriteTimeout time.Duration
  23. IdleTimeout time.Duration
  24. Tls TlsConfig
  25. }
  26. type DatabaseConfig struct {
  27. DSN string
  28. }
  29. type StripeConfig struct {
  30. APIKey string `mapstructure:"api_key" env:"STRIPE_API_KEY"`
  31. EndpointSecret string `mapstructure:"endpoint_secret" env:"STRIPE_WEBHOOK_SECRET"`
  32. }
  33. type OVHConfig struct {
  34. Endpoint string
  35. AppKey string
  36. AppSecret string
  37. ConsumerKey string
  38. }
  39. type MailerConfig struct {
  40. Identity string
  41. Username string
  42. Password string
  43. Host string
  44. Port string
  45. From string
  46. }
  47. type TlsConfig struct {
  48. CertFile string
  49. KeyFile string
  50. }
  51. type LoggingConfig struct {
  52. Level string
  53. Format string
  54. }
  55. type JWT struct {
  56. Secret string
  57. }
  58. func Load() (*Config, error) {
  59. viper.SetConfigName("config")
  60. viper.SetConfigType("yaml")
  61. viper.AddConfigPath(".")
  62. viper.AddConfigPath("/etc/byom/")
  63. // Set defaults
  64. viper.SetDefault("server.port", 8080)
  65. viper.SetDefault("server.readTimeout", "15s")
  66. viper.SetDefault("server.writeTimeout", "15s")
  67. viper.SetDefault("server.idleTimeout", "60s")
  68. viper.SetDefault("database.dsn", ".trash/trash.db")
  69. //generate random secret for jwt
  70. secret, err := GenerateRandomString(256)
  71. if err != nil {
  72. return nil, fmt.Errorf("error generating random string: %w", err)
  73. }
  74. viper.SetDefault("jwt.secret", secret)
  75. // Environment variables
  76. viper.AutomaticEnv()
  77. viper.SetEnvPrefix("BYOM")
  78. // Bind environment variables
  79. viper.BindEnv("stripe.api_key", "STRIPE_API_KEY")
  80. viper.BindEnv("stripe.endpoint_secret", "STRIPE_WEBHOOK_SECRET")
  81. // Set defaults if needed
  82. viper.SetDefault("stripe.api_key", "")
  83. viper.SetDefault("stripe.endpoint_secret", "")
  84. if err := viper.ReadInConfig(); err != nil {
  85. if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
  86. return nil, fmt.Errorf("error reading config: %w", err)
  87. }
  88. }
  89. var config Config
  90. if err := viper.Unmarshal(&config); err != nil {
  91. return nil, fmt.Errorf("error unmarshaling config: %w", err)
  92. }
  93. return &config, nil
  94. }
  95. func GenerateRandomString(n int) (string, error) {
  96. const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  97. ret := make([]byte, n)
  98. for i := 0; i < n; i++ {
  99. num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
  100. if err != nil {
  101. return "", err
  102. }
  103. ret[i] = letters[num.Int64()]
  104. }
  105. return string(ret), nil
  106. }