config.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package config
  2. import (
  3. "log"
  4. "time"
  5. "github.com/joeshaw/envdecode"
  6. )
  7. // Conf is the configuration for the Go server
  8. type Conf struct {
  9. Debug bool `env:"DEBUG,default=false"`
  10. Server ServerConf
  11. Db DBConf
  12. K8s K8sConf
  13. }
  14. // ServerConf is the server configuration
  15. type ServerConf struct {
  16. Port int `env:"SERVER_PORT,default=8080"`
  17. CookieName string `env:"COOKIE_NAME,default=porter"`
  18. CookieSecret []byte `env:"COOKIE_SECRET,default=secret"`
  19. TimeoutRead time.Duration `env:"SERVER_TIMEOUT_READ,default=5s"`
  20. TimeoutWrite time.Duration `env:"SERVER_TIMEOUT_WRITE,default=10s"`
  21. TimeoutIdle time.Duration `env:"SERVER_TIMEOUT_IDLE,default=15s"`
  22. }
  23. // DBConf is the database configuration: if generated from environment variables,
  24. // it assumes the default docker-compose configuration is used
  25. type DBConf struct {
  26. Host string `env:"DB_HOST,default=postgres"`
  27. Port int `env:"DB_PORT,default=5432"`
  28. Username string `env:"DB_USER,default=porter"`
  29. Password string `env:"DB_PASS,default=porter"`
  30. DbName string `env:"DB_NAME,default=porter"`
  31. SQLLite bool `env:"QUICK_START,default=false"`
  32. }
  33. // K8sConf is the global configuration for the k8s agents
  34. type K8sConf struct {
  35. IsTesting bool `env:"K8S_IS_TESTING,default=false"`
  36. }
  37. // FromEnv generates a configuration from environment variables
  38. func FromEnv() *Conf {
  39. var c Conf
  40. if err := envdecode.StrictDecode(&c); err != nil {
  41. log.Fatalf("Failed to decode: %s", err)
  42. }
  43. return &c
  44. }