config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "github.com/imgproxy/imgproxy/v3/server/responsewriter"
  10. )
  11. // Config represents HTTP server config
  12. type Config struct {
  13. Listen string // Address to listen on
  14. Network string // Network type (tcp, unix)
  15. Bind string // Bind address
  16. PathPrefix string // Path prefix for the server
  17. MaxClients int // Maximum number of concurrent clients
  18. ReadRequestTimeout time.Duration // Timeout for reading requests
  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. ResponseWriter responsewriter.Config // Response writer config
  27. // TODO: We are not sure where to put it yet
  28. FreeMemoryInterval time.Duration // Interval for freeing memory
  29. LogMemStats bool // Log memory stats
  30. }
  31. // NewDefaultConfig returns default config values
  32. func NewDefaultConfig() Config {
  33. return Config{
  34. Network: "tcp",
  35. Bind: ":8080",
  36. PathPrefix: "",
  37. MaxClients: 2048,
  38. ReadRequestTimeout: 10 * time.Second,
  39. KeepAliveTimeout: 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. ResponseWriter: responsewriter.NewDefaultConfig(),
  49. }
  50. }
  51. // LoadConfigFromEnv overrides current values with environment variables
  52. func LoadConfigFromEnv(c *Config) (*Config, error) {
  53. c = ensure.Ensure(c, NewDefaultConfig)
  54. c.Network = config.Network
  55. c.Bind = config.Bind
  56. c.PathPrefix = config.PathPrefix
  57. c.MaxClients = config.MaxClients
  58. c.ReadRequestTimeout = time.Duration(config.ReadRequestTimeout) * time.Second
  59. c.KeepAliveTimeout = time.Duration(config.KeepAliveTimeout) * time.Second
  60. c.GracefulTimeout = time.Duration(config.GracefulStopTimeout) * time.Second
  61. c.CORSAllowOrigin = config.AllowOrigin
  62. c.Secret = config.Secret
  63. c.DevelopmentErrorsMode = config.DevelopmentErrorsMode
  64. c.SocketReusePort = config.SoReuseport
  65. c.HealthCheckPath = config.HealthCheckPath
  66. c.FreeMemoryInterval = time.Duration(config.FreeMemoryInterval) * time.Second
  67. c.LogMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
  68. if _, err := responsewriter.LoadConfigFromEnv(&c.ResponseWriter); err != nil {
  69. return nil, err
  70. }
  71. return c, nil
  72. }
  73. // Validate checks that the config values are valid
  74. func (c *Config) Validate() error {
  75. if len(c.Bind) == 0 {
  76. return errors.New("bind address is not defined")
  77. }
  78. if c.MaxClients < 0 {
  79. return fmt.Errorf("max clients number should be greater than or equal 0, now - %d", c.MaxClients)
  80. }
  81. if c.ReadRequestTimeout <= 0 {
  82. return fmt.Errorf("read request timeout should be greater than 0, now - %d", c.ReadRequestTimeout)
  83. }
  84. if c.KeepAliveTimeout < 0 {
  85. return fmt.Errorf("keep alive timeout should be greater than or equal to 0, now - %d", c.KeepAliveTimeout)
  86. }
  87. if c.GracefulTimeout < 0 {
  88. return fmt.Errorf("graceful timeout should be greater than or equal to 0, now - %d", c.GracefulTimeout)
  89. }
  90. if c.FreeMemoryInterval <= 0 {
  91. return errors.New("free memory interval should be greater than zero")
  92. }
  93. return nil
  94. }