config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package processing
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. )
  7. // Config represents handler config
  8. type Config struct {
  9. PathPrefix string // Route path prefix
  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. PathPrefix: "",
  22. CookiePassthrough: false,
  23. ReportDownloadingErrors: true,
  24. LastModifiedEnabled: true,
  25. ETagEnabled: true,
  26. ReportIOErrors: false,
  27. FallbackImageHTTPCode: http.StatusOK,
  28. EnableDebugHeaders: false,
  29. }
  30. }
  31. // LoadFromEnv loads config from environment variables
  32. func LoadFromEnv(c *Config) (*Config, error) {
  33. c.PathPrefix = config.PathPrefix
  34. c.CookiePassthrough = config.CookiePassthrough
  35. c.ReportDownloadingErrors = config.ReportDownloadingErrors
  36. c.LastModifiedEnabled = config.LastModifiedEnabled
  37. c.ETagEnabled = config.ETagEnabled
  38. c.ReportIOErrors = config.ReportIOErrors
  39. c.FallbackImageHTTPCode = config.FallbackImageHTTPCode
  40. c.EnableDebugHeaders = config.EnableDebugHeaders
  41. return c, nil
  42. }
  43. // Validate checks configuration values
  44. func (c *Config) Validate() error {
  45. if c.FallbackImageHTTPCode != 0 && (c.FallbackImageHTTPCode < 100 || c.FallbackImageHTTPCode > 599) {
  46. return errors.New("fallback image HTTP code should be between 100 and 599")
  47. }
  48. return nil
  49. }