config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package fetcher
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/version"
  7. )
  8. // Config holds the configuration for the image fetcher.
  9. type Config struct {
  10. // UserAgent is the User-Agent header to use when fetching images.
  11. UserAgent string
  12. // DownloadTimeout is the timeout for downloading an image, in seconds.
  13. DownloadTimeout time.Duration
  14. // MaxRedirects is the maximum number of redirects to follow when fetching an image.
  15. MaxRedirects int
  16. }
  17. // NewDefaultConfig returns a new Config instance with default values.
  18. func NewDefaultConfig() *Config {
  19. return &Config{
  20. UserAgent: "imgproxy/" + version.Version,
  21. DownloadTimeout: 5 * time.Second,
  22. MaxRedirects: 10,
  23. }
  24. }
  25. // LoadFromEnv loads config variables from env
  26. func LoadFromEnv(c *Config) (*Config, error) {
  27. c.UserAgent = config.UserAgent
  28. c.DownloadTimeout = time.Duration(config.DownloadTimeout) * time.Second
  29. c.MaxRedirects = config.MaxRedirects
  30. return c, nil
  31. }
  32. // Validate checks config for errors
  33. func (c *Config) Validate() error {
  34. if len(c.UserAgent) == 0 {
  35. return errors.New("user agent cannot be empty")
  36. }
  37. if c.DownloadTimeout <= 0 {
  38. return errors.New("download timeout must be greater than 0")
  39. }
  40. return nil
  41. }