config.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. PathPrefix string // Route path prefix
  11. CookiePassthrough bool // Whether to passthrough cookies
  12. ReportDownloadingErrors bool // Whether to report downloading errors
  13. LastModifiedEnabled bool // Whether to enable Last-Modified
  14. ETagEnabled bool // Whether to enable ETag
  15. ReportIOErrors bool // Whether to report IO errors
  16. FallbackImageHTTPCode int // Fallback image HTTP status code
  17. EnableDebugHeaders bool // Whether to enable debug headers
  18. }
  19. // NewDefaultConfig creates a new configuration with defaults
  20. func NewDefaultConfig() Config {
  21. return Config{
  22. PathPrefix: "",
  23. CookiePassthrough: false,
  24. ReportDownloadingErrors: true,
  25. LastModifiedEnabled: true,
  26. ETagEnabled: true,
  27. ReportIOErrors: false,
  28. FallbackImageHTTPCode: http.StatusOK,
  29. EnableDebugHeaders: false,
  30. }
  31. }
  32. // LoadConfigFromEnv loads config from environment variables
  33. func LoadConfigFromEnv(c *Config) (*Config, error) {
  34. c = ensure.Ensure(c, NewDefaultConfig)
  35. c.PathPrefix = config.PathPrefix
  36. c.CookiePassthrough = config.CookiePassthrough
  37. c.ReportDownloadingErrors = config.ReportDownloadingErrors
  38. c.LastModifiedEnabled = config.LastModifiedEnabled
  39. c.ETagEnabled = config.ETagEnabled
  40. c.ReportIOErrors = config.ReportIOErrors
  41. c.FallbackImageHTTPCode = config.FallbackImageHTTPCode
  42. c.EnableDebugHeaders = config.EnableDebugHeaders
  43. return c, nil
  44. }
  45. // Validate checks configuration values
  46. func (c *Config) Validate() error {
  47. if c.FallbackImageHTTPCode != 0 && (c.FallbackImageHTTPCode < 100 || c.FallbackImageHTTPCode > 599) {
  48. return errors.New("fallback image HTTP code should be between 100 and 599")
  49. }
  50. return nil
  51. }