config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. SecretKey string
  31. WebhookSecret string
  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. if err := viper.ReadInConfig(); err != nil {
  79. if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
  80. return nil, fmt.Errorf("error reading config: %w", err)
  81. }
  82. }
  83. var config Config
  84. if err := viper.Unmarshal(&config); err != nil {
  85. return nil, fmt.Errorf("error unmarshaling config: %w", err)
  86. }
  87. return &config, nil
  88. }
  89. func GenerateRandomString(n int) (string, error) {
  90. const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  91. ret := make([]byte, n)
  92. for i := 0; i < n; i++ {
  93. num, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
  94. if err != nil {
  95. return "", err
  96. }
  97. ret[i] = letters[num.Int64()]
  98. }
  99. return string(ret), nil
  100. }