config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package server
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/imgproxy/imgproxy/v3/config"
  8. "github.com/imgproxy/imgproxy/v3/ensure"
  9. )
  10. // Config represents HTTP server config
  11. type Config struct {
  12. Listen string // Address to listen on
  13. Network string // Network type (tcp, unix)
  14. Bind string // Bind address
  15. PathPrefix string // Path prefix for the server
  16. MaxClients int // Maximum number of concurrent clients
  17. ReadRequestTimeout time.Duration // Timeout for reading requests
  18. WriteResponseTimeout time.Duration // Timeout for writing responses
  19. KeepAliveTimeout time.Duration // Timeout for keep-alive connections
  20. GracefulTimeout time.Duration // Timeout for graceful shutdown
  21. CORSAllowOrigin string // CORS allowed origin
  22. Secret string // Secret for authorization
  23. DevelopmentErrorsMode bool // Enable development mode for detailed error messages
  24. SocketReusePort bool // Enable SO_REUSEPORT socket option
  25. HealthCheckPath string // Health check path from config
  26. // TODO: We are not sure where to put it yet
  27. FreeMemoryInterval time.Duration // Interval for freeing memory
  28. LogMemStats bool // Log memory stats
  29. }
  30. // NewDefaultConfig returns default config values
  31. func NewDefaultConfig() Config {
  32. return Config{
  33. Network: "tcp",
  34. Bind: ":8080",
  35. PathPrefix: "",
  36. MaxClients: 2048,
  37. ReadRequestTimeout: 10 * time.Second,
  38. KeepAliveTimeout: 10 * time.Second,
  39. WriteResponseTimeout: 10 * time.Second,
  40. GracefulTimeout: 20 * time.Second,
  41. CORSAllowOrigin: "",
  42. Secret: "",
  43. DevelopmentErrorsMode: false,
  44. SocketReusePort: false,
  45. HealthCheckPath: "",
  46. FreeMemoryInterval: 10 * time.Second,
  47. LogMemStats: false,
  48. }
  49. }
  50. // LoadConfigFromEnv overrides current values with environment variables
  51. func LoadConfigFromEnv(c *Config) (*Config, error) {
  52. c = ensure.Ensure(c, NewDefaultConfig)
  53. c.Network = config.Network
  54. c.Bind = config.Bind
  55. c.PathPrefix = config.PathPrefix
  56. c.MaxClients = config.MaxClients
  57. c.ReadRequestTimeout = time.Duration(config.ReadRequestTimeout) * time.Second
  58. c.KeepAliveTimeout = time.Duration(config.KeepAliveTimeout) * time.Second
  59. c.GracefulTimeout = time.Duration(config.GracefulStopTimeout) * time.Second
  60. c.CORSAllowOrigin = config.AllowOrigin
  61. c.Secret = config.Secret
  62. c.DevelopmentErrorsMode = config.DevelopmentErrorsMode
  63. c.SocketReusePort = config.SoReuseport
  64. c.HealthCheckPath = config.HealthCheckPath
  65. c.FreeMemoryInterval = time.Duration(config.FreeMemoryInterval) * time.Second
  66. c.LogMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
  67. return c, nil
  68. }
  69. // Validate checks that the config values are valid
  70. func (c *Config) Validate() error {
  71. if len(c.Bind) == 0 {
  72. return errors.New("bind address is not defined")
  73. }
  74. if c.MaxClients < 0 {
  75. return fmt.Errorf("max clients number should be greater than or equal 0, now - %d", c.MaxClients)
  76. }
  77. if c.ReadRequestTimeout <= 0 {
  78. return fmt.Errorf("read request timeout should be greater than 0, now - %d", c.ReadRequestTimeout)
  79. }
  80. if c.WriteResponseTimeout <= 0 {
  81. return fmt.Errorf("write response timeout should be greater than 0, now - %d", c.WriteResponseTimeout)
  82. }
  83. if c.KeepAliveTimeout < 0 {
  84. return fmt.Errorf("keep alive timeout should be greater than or equal to 0, now - %d", c.KeepAliveTimeout)
  85. }
  86. if c.GracefulTimeout < 0 {
  87. return fmt.Errorf("graceful timeout should be greater than or equal to 0, now - %d", c.GracefulTimeout)
  88. }
  89. if c.FreeMemoryInterval <= 0 {
  90. return errors.New("free memory interval should be greater than zero")
  91. }
  92. return nil
  93. }