config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package responsewriter
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. "github.com/imgproxy/imgproxy/v3/ensure"
  7. "github.com/imgproxy/imgproxy/v3/env"
  8. )
  9. var (
  10. IMGPROXY_SET_CANONICAL_HEADER = env.Describe("IMGPROXY_SET_CANONICAL_HEADER", "boolean")
  11. IMGPROXY_TTL = env.Describe("IMGPROXY_TTL", "seconds >= 0")
  12. IMGPROXY_FALLBACK_IMAGE_TTL = env.Describe("IMGPROXY_FALLBACK_IMAGE_TTL", "seconds >= 0")
  13. IMGPROXY_CACHE_CONTROL_PASSTHROUGH = env.Describe("IMGPROXY_CACHE_CONTROL_PASSTHROUGH", "boolean")
  14. IMGPROXY_WRITE_RESPONSE_TIMEOUT = env.Describe("IMGPROXY_WRITE_RESPONSE_TIMEOUT", "seconds > 0")
  15. // NOTE: These are referenced here to determine if we need to set the Vary header
  16. // Unfotunately, we can not reuse them from optionsparser package due to import cycle
  17. IMGPROXY_AUTO_WEBP = env.Describe("IMGPROXY_AUTO_WEBP", "boolean")
  18. IMGPROXY_AUTO_AVIF = env.Describe("IMGPROXY_AUTO_AVIF", "boolean")
  19. IMGPROXY_AUTO_JXL = env.Describe("IMGPROXY_AUTO_JXL", "boolean")
  20. IMGPROXY_ENFORCE_WEBP = env.Describe("IMGPROXY_ENFORCE_WEBP", "boolean")
  21. IMGPROXY_ENFORCE_AVIF = env.Describe("IMGPROXY_ENFORCE_AVIF", "boolean")
  22. IMGPROXY_ENFORCE_JXL = env.Describe("IMGPROXY_ENFORCE_JXL", "boolean")
  23. IMGPROXY_ENABLE_CLIENT_HINTS = env.Describe("IMGPROXY_ENABLE_CLIENT_HINTS", "boolean")
  24. )
  25. // Config holds configuration for response writer
  26. type Config struct {
  27. SetCanonicalHeader bool // Indicates whether to set the canonical header
  28. DefaultTTL int // Default Cache-Control max-age= value for cached images
  29. FallbackImageTTL int // TTL for images served as fallbacks
  30. CacheControlPassthrough bool // Passthrough the Cache-Control from the original response
  31. VaryValue string // Value for Vary header
  32. WriteResponseTimeout time.Duration // Timeout for response write operations
  33. }
  34. // NewDefaultConfig returns a new Config instance with default values.
  35. func NewDefaultConfig() Config {
  36. return Config{
  37. SetCanonicalHeader: false,
  38. DefaultTTL: 31_536_000,
  39. FallbackImageTTL: 0,
  40. CacheControlPassthrough: false,
  41. VaryValue: "",
  42. WriteResponseTimeout: 10 * time.Second,
  43. }
  44. }
  45. // LoadConfigFromEnv overrides configuration variables from environment
  46. func LoadConfigFromEnv(c *Config) (*Config, error) {
  47. c = ensure.Ensure(c, NewDefaultConfig)
  48. err := errors.Join(
  49. env.Bool(&c.SetCanonicalHeader, IMGPROXY_SET_CANONICAL_HEADER),
  50. env.Int(&c.DefaultTTL, IMGPROXY_TTL),
  51. env.Int(&c.FallbackImageTTL, IMGPROXY_FALLBACK_IMAGE_TTL),
  52. env.Bool(&c.CacheControlPassthrough, IMGPROXY_CACHE_CONTROL_PASSTHROUGH),
  53. env.Duration(&c.WriteResponseTimeout, IMGPROXY_WRITE_RESPONSE_TIMEOUT),
  54. )
  55. if err != nil {
  56. return nil, err
  57. }
  58. vary := make([]string, 0)
  59. var ok bool
  60. if err, ok = c.envEnableFormatDetection(); err != nil {
  61. return nil, err
  62. }
  63. if ok {
  64. vary = append(vary, "Accept")
  65. }
  66. if err, ok = c.envEnableClientHints(); err != nil {
  67. return nil, err
  68. }
  69. if ok {
  70. vary = append(vary, "Sec-CH-DPR", "DPR", "Sec-CH-Width", "Width")
  71. }
  72. c.VaryValue = strings.Join(vary, ", ")
  73. return c, nil
  74. }
  75. // envEnableFormatDetection checks if any of the format detection options are enabled
  76. func (c *Config) envEnableFormatDetection() (error, bool) {
  77. var autoWebp, enforceWebp, autoAvif, enforceAvif, autoJxl, enforceJxl bool
  78. // We won't need those variables in runtime, hence, we could
  79. // read them here once into local variables
  80. err := errors.Join(
  81. env.Bool(&autoWebp, IMGPROXY_AUTO_WEBP),
  82. env.Bool(&enforceWebp, IMGPROXY_ENFORCE_WEBP),
  83. env.Bool(&autoAvif, IMGPROXY_AUTO_AVIF),
  84. env.Bool(&enforceAvif, IMGPROXY_ENFORCE_AVIF),
  85. env.Bool(&autoJxl, IMGPROXY_AUTO_JXL),
  86. env.Bool(&enforceJxl, IMGPROXY_ENFORCE_JXL),
  87. )
  88. if err != nil {
  89. return err, false
  90. }
  91. return nil, autoWebp ||
  92. enforceWebp ||
  93. autoAvif ||
  94. enforceAvif ||
  95. autoJxl ||
  96. enforceJxl
  97. }
  98. // envEnableClientHints checks if client hints are enabled
  99. func (c *Config) envEnableClientHints() (err error, ok bool) {
  100. err = env.Bool(&ok, IMGPROXY_ENABLE_CLIENT_HINTS)
  101. return
  102. }
  103. // Validate checks config for errors
  104. func (c *Config) Validate() error {
  105. if c.DefaultTTL < 0 {
  106. return IMGPROXY_TTL.ErrorNegative()
  107. }
  108. if c.FallbackImageTTL < 0 {
  109. return IMGPROXY_FALLBACK_IMAGE_TTL.ErrorNegative()
  110. }
  111. if c.WriteResponseTimeout <= 0 {
  112. return IMGPROXY_WRITE_RESPONSE_TIMEOUT.ErrorZeroOrNegative()
  113. }
  114. return nil
  115. }