config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // config.go is just a shortcut for common.Config which helps to
  2. // avoid importing of the `common` package directly.
  3. package transport
  4. import (
  5. "errors"
  6. "os"
  7. "github.com/imgproxy/imgproxy/v3/ensure"
  8. "github.com/imgproxy/imgproxy/v3/env"
  9. "github.com/imgproxy/imgproxy/v3/fetcher/transport/azure"
  10. "github.com/imgproxy/imgproxy/v3/fetcher/transport/fs"
  11. "github.com/imgproxy/imgproxy/v3/fetcher/transport/gcs"
  12. "github.com/imgproxy/imgproxy/v3/fetcher/transport/generichttp"
  13. "github.com/imgproxy/imgproxy/v3/fetcher/transport/s3"
  14. "github.com/imgproxy/imgproxy/v3/fetcher/transport/swift"
  15. )
  16. var (
  17. IMGPROXY_USE_ABS = env.Describe("IMGPROXY_USE_ABS", "boolean")
  18. IMGPROXY_USE_GCS = env.Describe("IMGPROXY_GCS_ENABLED", "boolean")
  19. IMGPROXY_USE_S3 = env.Describe("IMGPROXY_USE_S3", "boolean")
  20. IMGPROXY_USE_SWIFT = env.Describe("IMGPROXY_USE_SWIFT", "boolean")
  21. IMGPROXY_SOURCE_URL_QUERY_SEPARATOR = env.Describe("IMGPROXY_SOURCE_URL_QUERY_SEPARATOR", "string")
  22. )
  23. // Config represents configuration of the transport package
  24. type Config struct {
  25. HTTP generichttp.Config
  26. Local fs.Config
  27. ABSEnabled bool
  28. ABS azure.Config
  29. GCSEnabled bool
  30. GCS gcs.Config
  31. S3Enabled bool
  32. S3 s3.Config
  33. SwiftEnabled bool
  34. Swift swift.Config
  35. // query string separator (see docs). Unfortunately, we'll have to pass this
  36. // to each transport which needs it as the consturctor parameter. Otherwise,
  37. // we would have to add it to each transport config struct.
  38. SourceURLQuerySeparator string
  39. }
  40. // NewDefaultConfig returns a new default transport configuration
  41. func NewDefaultConfig() Config {
  42. return Config{
  43. HTTP: generichttp.NewDefaultConfig(),
  44. Local: fs.NewDefaultConfig(),
  45. ABSEnabled: false,
  46. ABS: azure.NewDefaultConfig(),
  47. GCSEnabled: false,
  48. GCS: gcs.NewDefaultConfig(),
  49. S3Enabled: false,
  50. S3: s3.NewDefaultConfig(),
  51. SwiftEnabled: false,
  52. Swift: swift.NewDefaultConfig(),
  53. SourceURLQuerySeparator: "?", // default is ?, but can be overriden with empty
  54. }
  55. }
  56. // LoadConfigFromEnv loads transport configuration from environment variables
  57. func LoadConfigFromEnv(c *Config) (*Config, error) {
  58. c = ensure.Ensure(c, NewDefaultConfig)
  59. _, genericErr := generichttp.LoadConfigFromEnv(&c.HTTP)
  60. _, localErr := fs.LoadConfigFromEnv(&c.Local)
  61. _, azureErr := azure.LoadConfigFromEnv(&c.ABS)
  62. _, gcsErr := gcs.LoadConfigFromEnv(&c.GCS)
  63. _, s3Err := s3.LoadConfigFromEnv(&c.S3)
  64. _, swiftErr := swift.LoadConfigFromEnv(&c.Swift)
  65. err := errors.Join(
  66. genericErr,
  67. localErr,
  68. azureErr,
  69. gcsErr,
  70. s3Err,
  71. swiftErr,
  72. env.Bool(&c.ABSEnabled, IMGPROXY_USE_ABS),
  73. env.Bool(&c.GCSEnabled, IMGPROXY_USE_GCS),
  74. env.Bool(&c.S3Enabled, IMGPROXY_USE_S3),
  75. env.Bool(&c.SwiftEnabled, IMGPROXY_USE_SWIFT),
  76. )
  77. // empty value is a valid value for this separator, we can't rely on env.String,
  78. // which skips empty values
  79. if s, ok := os.LookupEnv(IMGPROXY_SOURCE_URL_QUERY_SEPARATOR.Name); ok {
  80. c.SourceURLQuerySeparator = s
  81. }
  82. return c, err
  83. }
  84. func (c *Config) Validate() error {
  85. // We won't validate upstream config here: they might not be used
  86. return nil
  87. }