config.go 3.2 KB

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