config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package headerwriter
  2. import (
  3. "fmt"
  4. "github.com/imgproxy/imgproxy/v3/config"
  5. "github.com/imgproxy/imgproxy/v3/ensure"
  6. )
  7. // Config is the package-local configuration
  8. type Config struct {
  9. SetCanonicalHeader bool // Indicates whether to set the canonical header
  10. DefaultTTL int // Default Cache-Control max-age= value for cached images
  11. FallbackImageTTL int // TTL for images served as fallbacks
  12. CacheControlPassthrough bool // Passthrough the Cache-Control from the original response
  13. EnableClientHints bool // Enable Vary header
  14. SetVaryAccept bool // Whether to include Accept in Vary header
  15. }
  16. // NewDefaultConfig returns a new Config instance with default values.
  17. func NewDefaultConfig() Config {
  18. return Config{
  19. SetCanonicalHeader: false,
  20. DefaultTTL: 31536000,
  21. FallbackImageTTL: 0,
  22. CacheControlPassthrough: false,
  23. EnableClientHints: false,
  24. SetVaryAccept: false,
  25. }
  26. }
  27. // LoadConfigFromEnv overrides configuration variables from environment
  28. func LoadConfigFromEnv(c *Config) (*Config, error) {
  29. c = ensure.Ensure(c, NewDefaultConfig)
  30. c.SetCanonicalHeader = config.SetCanonicalHeader
  31. c.DefaultTTL = config.TTL
  32. c.FallbackImageTTL = config.FallbackImageTTL
  33. c.CacheControlPassthrough = config.CacheControlPassthrough
  34. c.EnableClientHints = config.EnableClientHints
  35. c.SetVaryAccept = config.AutoWebp ||
  36. config.EnforceWebp ||
  37. config.AutoAvif ||
  38. config.EnforceAvif ||
  39. config.AutoJxl ||
  40. config.EnforceJxl
  41. return c, nil
  42. }
  43. // Validate checks config for errors
  44. func (c *Config) Validate() error {
  45. if c.DefaultTTL < 0 {
  46. return fmt.Errorf("image TTL should be greater than or equal to 0, now - %d", c.DefaultTTL)
  47. }
  48. if c.FallbackImageTTL < 0 {
  49. return fmt.Errorf("fallback image TTL should be greater than or equal to 0, now - %d", c.FallbackImageTTL)
  50. }
  51. return nil
  52. }