config.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. ServerURL string `env:"SERVER_URL,default=http://localhost:8080"`
  17. Port int `env:"SERVER_PORT,default=8080"`
  18. StaticFilePath string `env:"STATIC_FILE_PATH,default=/porter/static"`
  19. CookieName string `env:"COOKIE_NAME,default=porter"`
  20. CookieSecret []byte `env:"COOKIE_SECRET,default=secret"`
  21. TimeoutRead time.Duration `env:"SERVER_TIMEOUT_READ,default=5s"`
  22. TimeoutWrite time.Duration `env:"SERVER_TIMEOUT_WRITE,default=10s"`
  23. TimeoutIdle time.Duration `env:"SERVER_TIMEOUT_IDLE,default=15s"`
  24. GithubClientID string `env:"GITHUB_CLIENT_ID"`
  25. GithubClientSecret string `env:"GITHUB_CLIENT_SECRET"`
  26. }
  27. // DBConf is the database configuration: if generated from environment variables,
  28. // it assumes the default docker-compose configuration is used
  29. type DBConf struct {
  30. // EncryptionKey is the key to use for sensitive values that are encrypted at rest
  31. EncryptionKey string `env:"ENCRYPTION_KEY,default=__random_strong_encryption_key__"`
  32. Host string `env:"DB_HOST,default=postgres"`
  33. Port int `env:"DB_PORT,default=5432"`
  34. Username string `env:"DB_USER,default=porter"`
  35. Password string `env:"DB_PASS,default=porter"`
  36. DbName string `env:"DB_NAME,default=porter"`
  37. SQLLite bool `env:"SQL_LITE,default=false"`
  38. SQLLitePath string `env:"SQL_LITE_PATH,default=/porter/porter.db"`
  39. }
  40. // K8sConf is the global configuration for the k8s agents
  41. type K8sConf struct {
  42. IsTesting bool `env:"K8S_IS_TESTING,default=false"`
  43. }
  44. // FromEnv generates a configuration from environment variables
  45. func FromEnv() *Conf {
  46. var c Conf
  47. if err := envdecode.StrictDecode(&c); err != nil {
  48. log.Fatalf("Failed to decode: %s", err)
  49. }
  50. return &c
  51. }