config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package config
  5. import (
  6. "os"
  7. "strconv"
  8. )
  9. const (
  10. defaultBaseURL = "http://localhost"
  11. defaultDatabaseURL = "postgres://postgres:postgres@localhost/miniflux2?sslmode=disable"
  12. defaultWorkerPoolSize = 5
  13. defaultPollingFrequency = 60
  14. defaultBatchSize = 10
  15. defaultDatabaseMaxConns = 20
  16. defaultListenAddr = "127.0.0.1:8080"
  17. defaultCertFile = ""
  18. defaultKeyFile = ""
  19. defaultCertDomain = ""
  20. defaultCertCache = "/tmp/cert_cache"
  21. defaultSessionCleanupFrequency = 24
  22. )
  23. // Config manages configuration parameters.
  24. type Config struct {
  25. IsHTTPS bool
  26. }
  27. func (c *Config) get(key, fallback string) string {
  28. value := os.Getenv(key)
  29. if value == "" {
  30. return fallback
  31. }
  32. return value
  33. }
  34. func (c *Config) getInt(key string, fallback int) int {
  35. value := os.Getenv(key)
  36. if value == "" {
  37. return fallback
  38. }
  39. v, _ := strconv.Atoi(value)
  40. return v
  41. }
  42. // BaseURL returns the application base URL.
  43. func (c *Config) BaseURL() string {
  44. return c.get("BASE_URL", defaultBaseURL)
  45. }
  46. // DatabaseURL returns the database URL.
  47. func (c *Config) DatabaseURL() string {
  48. return c.get("DATABASE_URL", defaultDatabaseURL)
  49. }
  50. // DatabaseMaxConnections returns the number of maximum database connections.
  51. func (c *Config) DatabaseMaxConnections() int {
  52. return c.getInt("DATABASE_MAX_CONNS", defaultDatabaseMaxConns)
  53. }
  54. // ListenAddr returns the listen address for the HTTP server.
  55. func (c *Config) ListenAddr() string {
  56. return c.get("LISTEN_ADDR", defaultListenAddr)
  57. }
  58. // CertFile returns the SSL certificate filename if any.
  59. func (c *Config) CertFile() string {
  60. return c.get("CERT_FILE", defaultCertFile)
  61. }
  62. // KeyFile returns the private key filename for custom SSL certificate.
  63. func (c *Config) KeyFile() string {
  64. return c.get("KEY_FILE", defaultKeyFile)
  65. }
  66. // CertDomain returns the domain to use for Let's Encrypt certificate.
  67. func (c *Config) CertDomain() string {
  68. return c.get("CERT_DOMAIN", defaultCertDomain)
  69. }
  70. // CertCache returns the directory to use for Let's Encrypt session cache.
  71. func (c *Config) CertCache() string {
  72. return c.get("CERT_CACHE", defaultCertCache)
  73. }
  74. // SessionCleanupFrequency returns the interval for session cleanup.
  75. func (c *Config) SessionCleanupFrequency() int {
  76. return c.getInt("SESSION_CLEANUP_FREQUENCY", defaultSessionCleanupFrequency)
  77. }
  78. // WorkerPoolSize returns the number of background worker.
  79. func (c *Config) WorkerPoolSize() int {
  80. return c.getInt("WORKER_POOL_SIZE", defaultWorkerPoolSize)
  81. }
  82. // PollingFrequency returns the interval to refresh feeds in the background.
  83. func (c *Config) PollingFrequency() int {
  84. return c.getInt("POLLING_FREQUENCY", defaultPollingFrequency)
  85. }
  86. // BatchSize returns the number of feeds to send for background processing.
  87. func (c *Config) BatchSize() int {
  88. return c.getInt("BATCH_SIZE", defaultBatchSize)
  89. }
  90. // IsOAuth2UserCreationAllowed returns true if user creation is allowed for OAuth2 users.
  91. func (c *Config) IsOAuth2UserCreationAllowed() bool {
  92. return c.getInt("OAUTH2_USER_CREATION", 0) == 1
  93. }
  94. // OAuth2ClientID returns the OAuth2 Client ID.
  95. func (c *Config) OAuth2ClientID() string {
  96. return c.get("OAUTH2_CLIENT_ID", "")
  97. }
  98. // OAuth2ClientSecret returns the OAuth2 client secret.
  99. func (c *Config) OAuth2ClientSecret() string {
  100. return c.get("OAUTH2_CLIENT_SECRET", "")
  101. }
  102. // OAuth2RedirectURL returns the OAuth2 redirect URL.
  103. func (c *Config) OAuth2RedirectURL() string {
  104. return c.get("OAUTH2_REDIRECT_URL", "")
  105. }
  106. // OAuth2Provider returns the name of the OAuth2 provider configured.
  107. func (c *Config) OAuth2Provider() string {
  108. return c.get("OAUTH2_PROVIDER", "")
  109. }
  110. // NewConfig returns a new Config.
  111. func NewConfig() *Config {
  112. return &Config{IsHTTPS: os.Getenv("HTTPS") != ""}
  113. }