config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package processing
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/ensure"
  7. )
  8. // Config represents handler config
  9. type Config struct {
  10. CookiePassthrough bool // Whether to passthrough cookies
  11. ReportDownloadingErrors bool // Whether to report downloading errors
  12. LastModifiedEnabled bool // Whether to enable Last-Modified
  13. ETagEnabled bool // Whether to enable ETag
  14. ReportIOErrors bool // Whether to report IO errors
  15. FallbackImageHTTPCode int // Fallback image HTTP status code
  16. EnableDebugHeaders bool // Whether to enable debug headers
  17. }
  18. // NewDefaultConfig creates a new configuration with defaults
  19. func NewDefaultConfig() Config {
  20. return Config{
  21. CookiePassthrough: false,
  22. ReportDownloadingErrors: true,
  23. LastModifiedEnabled: true,
  24. ETagEnabled: true,
  25. ReportIOErrors: false,
  26. FallbackImageHTTPCode: http.StatusOK,
  27. EnableDebugHeaders: false,
  28. }
  29. }
  30. // LoadConfigFromEnv loads config from environment variables
  31. func LoadConfigFromEnv(c *Config) (*Config, error) {
  32. c = ensure.Ensure(c, NewDefaultConfig)
  33. c.CookiePassthrough = config.CookiePassthrough
  34. c.ReportDownloadingErrors = config.ReportDownloadingErrors
  35. c.LastModifiedEnabled = config.LastModifiedEnabled
  36. c.ETagEnabled = config.ETagEnabled
  37. c.ReportIOErrors = config.ReportIOErrors
  38. c.FallbackImageHTTPCode = config.FallbackImageHTTPCode
  39. c.EnableDebugHeaders = config.EnableDebugHeaders
  40. return c, nil
  41. }
  42. // Validate checks configuration values
  43. func (c *Config) Validate() error {
  44. if c.FallbackImageHTTPCode != 0 && (c.FallbackImageHTTPCode < 100 || c.FallbackImageHTTPCode > 599) {
  45. return errors.New("fallback image HTTP code should be between 100 and 599")
  46. }
  47. return nil
  48. }