浏览代码

Allow using LoadConfigFromEnv with nil config

DarthSim 1 月之前
父节点
当前提交
4789810b18

+ 8 - 0
auximageprovider/static_config.go

@@ -20,6 +20,10 @@ func NewDefaultStaticConfig() *StaticConfig {
 
 
 // LoadWatermarkStaticConfigFromEnv loads the watermark configuration from the environment
 // LoadWatermarkStaticConfigFromEnv loads the watermark configuration from the environment
 func LoadWatermarkStaticConfigFromEnv(c *StaticConfig) (*StaticConfig, error) {
 func LoadWatermarkStaticConfigFromEnv(c *StaticConfig) (*StaticConfig, error) {
+	if c == nil {
+		c = NewDefaultStaticConfig()
+	}
+
 	c.Base64Data = config.WatermarkData
 	c.Base64Data = config.WatermarkData
 	c.Path = config.WatermarkPath
 	c.Path = config.WatermarkPath
 	c.URL = config.WatermarkURL
 	c.URL = config.WatermarkURL
@@ -29,6 +33,10 @@ func LoadWatermarkStaticConfigFromEnv(c *StaticConfig) (*StaticConfig, error) {
 
 
 // LoadFallbackStaticConfigFromEnv loads the fallback configuration from the environment
 // LoadFallbackStaticConfigFromEnv loads the fallback configuration from the environment
 func LoadFallbackStaticConfigFromEnv(c *StaticConfig) (*StaticConfig, error) {
 func LoadFallbackStaticConfigFromEnv(c *StaticConfig) (*StaticConfig, error) {
+	if c == nil {
+		c = NewDefaultStaticConfig()
+	}
+
 	c.Base64Data = config.FallbackImageData
 	c.Base64Data = config.FallbackImageData
 	c.Path = config.FallbackImagePath
 	c.Path = config.FallbackImagePath
 	c.URL = config.FallbackImageURL
 	c.URL = config.FallbackImageURL

+ 5 - 0
fetcher/config.go

@@ -29,9 +29,14 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads config variables from env
 // LoadConfigFromEnv loads config variables from env
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.UserAgent = config.UserAgent
 	c.UserAgent = config.UserAgent
 	c.DownloadTimeout = time.Duration(config.DownloadTimeout) * time.Second
 	c.DownloadTimeout = time.Duration(config.DownloadTimeout) * time.Second
 	c.MaxRedirects = config.MaxRedirects
 	c.MaxRedirects = config.MaxRedirects
+
 	return c, nil
 	return c, nil
 }
 }
 
 

+ 4 - 0
handlers/processing/config.go

@@ -35,6 +35,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads config from environment variables
 // LoadConfigFromEnv loads config from environment variables
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.PathPrefix = config.PathPrefix
 	c.PathPrefix = config.PathPrefix
 	c.CookiePassthrough = config.CookiePassthrough
 	c.CookiePassthrough = config.CookiePassthrough
 	c.ReportDownloadingErrors = config.ReportDownloadingErrors
 	c.ReportDownloadingErrors = config.ReportDownloadingErrors

+ 5 - 0
handlers/stream/config.go

@@ -40,7 +40,12 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads config variables from environment
 // LoadConfigFromEnv loads config variables from environment
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.CookiePassthrough = config.CookiePassthrough
 	c.CookiePassthrough = config.CookiePassthrough
+
 	return c, nil
 	return c, nil
 }
 }
 
 

+ 4 - 4
handlers/stream/handler_test.go

@@ -49,7 +49,7 @@ func (s *HandlerTestSuite) SetupTest() {
 	config.Reset()
 	config.Reset()
 	config.AllowLoopbackSourceAddresses = true
 	config.AllowLoopbackSourceAddresses = true
 
 
-	trc, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+	trc, err := transport.LoadConfigFromEnv(nil)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
 	tr, err := transport.New(trc)
 	tr, err := transport.New(trc)
@@ -349,7 +349,7 @@ func (s *HandlerTestSuite) TestHandlerCacheControl() {
 			}))
 			}))
 			defer ts.Close()
 			defer ts.Close()
 
 
-			trc, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+			trc, err := transport.LoadConfigFromEnv(nil)
 			s.Require().NoError(err)
 			s.Require().NoError(err)
 
 
 			// Create new handler with updated config for each test
 			// Create new handler with updated config for each test
@@ -446,7 +446,7 @@ func (s *HandlerTestSuite) TestHandlerErrorResponse() {
 
 
 // TestHandlerCookiePassthrough tests the cookie passthrough behavior of the streaming service.
 // TestHandlerCookiePassthrough tests the cookie passthrough behavior of the streaming service.
 func (s *HandlerTestSuite) TestHandlerCookiePassthrough() {
 func (s *HandlerTestSuite) TestHandlerCookiePassthrough() {
-	trc, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+	trc, err := transport.LoadConfigFromEnv(nil)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
 	// Create new handler with updated config
 	// Create new handler with updated config
@@ -506,7 +506,7 @@ func (s *HandlerTestSuite) TestHandlerCanonicalHeader() {
 	defer ts.Close()
 	defer ts.Close()
 
 
 	for _, sc := range []bool{true, false} {
 	for _, sc := range []bool{true, false} {
-		trc, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+		trc, err := transport.LoadConfigFromEnv(nil)
 		s.Require().NoError(err)
 		s.Require().NoError(err)
 
 
 		// Create new handler with updated config
 		// Create new handler with updated config

+ 4 - 0
headerwriter/config.go

@@ -30,6 +30,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv overrides configuration variables from environment
 // LoadConfigFromEnv overrides configuration variables from environment
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.SetCanonicalHeader = config.SetCanonicalHeader
 	c.SetCanonicalHeader = config.SetCanonicalHeader
 	c.DefaultTTL = config.TTL
 	c.DefaultTTL = config.TTL
 	c.FallbackImageTTL = config.FallbackImageTTL
 	c.FallbackImageTTL = config.FallbackImageTTL

+ 2 - 2
imagedata/image_data_test.go

@@ -70,13 +70,13 @@ func (s *ImageDataTestSuite) SetupSuite() {
 		rw.Write(data)
 		rw.Write(data)
 	}))
 	}))
 
 
-	ctr, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+	ctr, err := transport.LoadConfigFromEnv(nil)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
 	ts, err := transport.New(ctr)
 	ts, err := transport.New(ctr)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
-	c, err := fetcher.LoadConfigFromEnv(fetcher.NewDefaultConfig())
+	c, err := fetcher.LoadConfigFromEnv(nil)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
 	fetcher, err := fetcher.New(ts, c)
 	fetcher, err := fetcher.New(ts, c)

+ 9 - 11
main.go

@@ -46,7 +46,7 @@ const (
 
 
 func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) error {
 func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) error {
 	// NOTE: This is temporary, will be moved level up at once
 	// NOTE: This is temporary, will be moved level up at once
-	hwc, err := headerwriter.LoadConfigFromEnv(headerwriter.NewDefaultConfig())
+	hwc, err := headerwriter.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
@@ -56,12 +56,12 @@ func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request)
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	sc, err := stream.LoadConfigFromEnv(stream.NewDefaultConfig())
+	sc, err := stream.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	tcfg, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+	tcfg, err := transport.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
@@ -71,7 +71,7 @@ func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request)
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	fc, err := fetcher.LoadConfigFromEnv(fetcher.NewDefaultConfig())
+	fc, err := fetcher.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
@@ -88,12 +88,12 @@ func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request)
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	phc, err := processingHandler.LoadConfigFromEnv(processingHandler.NewDefaultConfig())
+	phc, err := processingHandler.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	semc, err := semaphores.LoadConfigFromEnv(semaphores.NewDefaultConfig())
+	semc, err := semaphores.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
@@ -103,8 +103,7 @@ func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request)
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	fic := auximageprovider.NewDefaultStaticConfig()
-	fic, err = auximageprovider.LoadFallbackStaticConfigFromEnv(fic)
+	fic, err := auximageprovider.LoadFallbackStaticConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
@@ -119,8 +118,7 @@ func callHandleProcessing(reqID string, rw http.ResponseWriter, r *http.Request)
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
 
 
-	wic := auximageprovider.NewDefaultStaticConfig()
-	wic, err = auximageprovider.LoadWatermarkStaticConfigFromEnv(wic)
+	wic, err := auximageprovider.LoadWatermarkStaticConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 		return ierrors.Wrap(err, 0, ierrors.WithCategory(categoryConfig))
 	}
 	}
@@ -240,7 +238,7 @@ func run(ctx context.Context) error {
 		return err
 		return err
 	}
 	}
 
 
-	cfg, err := server.LoadConfigFromEnv(server.NewDefaultConfig())
+	cfg, err := server.LoadConfigFromEnv(nil)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}

+ 2 - 2
processing_handler_test.go

@@ -108,13 +108,13 @@ func (s *ProcessingHandlerTestSuite) readTestImageData(name string) imagedata.Im
 	// because currently configuration is changed via env vars
 	// because currently configuration is changed via env vars
 	// or config. We need to pick up those config changes.
 	// or config. We need to pick up those config changes.
 	// This will be addressed in the next PR
 	// This will be addressed in the next PR
-	trc, err := transport.LoadConfigFromEnv(transport.NewDefaultConfig())
+	trc, err := transport.LoadConfigFromEnv(nil)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
 	tr, err := transport.New(trc)
 	tr, err := transport.New(trc)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
-	fc, err := fetcher.LoadConfigFromEnv(fetcher.NewDefaultConfig())
+	fc, err := fetcher.LoadConfigFromEnv(nil)
 	s.Require().NoError(err)
 	s.Require().NoError(err)
 
 
 	f, err := fetcher.New(tr, fc)
 	f, err := fetcher.New(tr, fc)

+ 4 - 0
semaphores/config.go

@@ -23,6 +23,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads config from environment variables
 // LoadConfigFromEnv loads config from environment variables
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.RequestsQueueSize = config.RequestsQueueSize
 	c.RequestsQueueSize = config.RequestsQueueSize
 	c.Workers = config.Workers
 	c.Workers = config.Workers
 
 

+ 4 - 0
server/config.go

@@ -52,6 +52,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv overrides current values with environment variables
 // LoadConfigFromEnv overrides current values with environment variables
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.Network = config.Network
 	c.Network = config.Network
 	c.Bind = config.Bind
 	c.Bind = config.Bind
 	c.PathPrefix = config.PathPrefix
 	c.PathPrefix = config.PathPrefix

+ 4 - 0
transport/azure/config.go

@@ -24,6 +24,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads configuration from the global config package
 // LoadConfigFromEnv loads configuration from the global config package
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.Name = config.ABSName
 	c.Name = config.ABSName
 	c.Endpoint = config.ABSEndpoint
 	c.Endpoint = config.ABSEndpoint
 	c.Key = config.ABSKey
 	c.Key = config.ABSKey

+ 4 - 0
transport/config.go

@@ -49,6 +49,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads transport configuration from environment variables
 // LoadConfigFromEnv loads transport configuration from environment variables
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	var err error
 	var err error
 
 
 	if c.HTTP, err = generichttp.LoadConfigFromEnv(c.HTTP); err != nil {
 	if c.HTTP, err = generichttp.LoadConfigFromEnv(c.HTTP); err != nil {

+ 4 - 0
transport/fs/config.go

@@ -22,6 +22,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads configuration from the global config package
 // LoadConfigFromEnv loads configuration from the global config package
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.Root = config.LocalFileSystemRoot
 	c.Root = config.LocalFileSystemRoot
 
 
 	return c, nil
 	return c, nil

+ 5 - 0
transport/gcs/config.go

@@ -18,8 +18,13 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads configuration from the global config package
 // LoadConfigFromEnv loads configuration from the global config package
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.Key = config.GCSKey
 	c.Key = config.GCSKey
 	c.Endpoint = config.GCSEndpoint
 	c.Endpoint = config.GCSEndpoint
+
 	return c, nil
 	return c, nil
 }
 }
 
 

+ 4 - 0
transport/generichttp/config.go

@@ -23,6 +23,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads configuration from the global config package
 // LoadConfigFromEnv loads configuration from the global config package
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.ClientKeepAliveTimeout = time.Duration(config.ClientKeepAliveTimeout) * time.Second
 	c.ClientKeepAliveTimeout = time.Duration(config.ClientKeepAliveTimeout) * time.Second
 	c.IgnoreSslVerification = config.IgnoreSslVerification
 	c.IgnoreSslVerification = config.IgnoreSslVerification
 
 

+ 4 - 0
transport/s3/config.go

@@ -26,6 +26,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads configuration from the global config package
 // LoadConfigFromEnv loads configuration from the global config package
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.Region = config.S3Region
 	c.Region = config.S3Region
 	c.Endpoint = config.S3Endpoint
 	c.Endpoint = config.S3Endpoint
 	c.EndpointUsePathStyle = config.S3EndpointUsePathStyle
 	c.EndpointUsePathStyle = config.S3EndpointUsePathStyle

+ 4 - 0
transport/swift/config.go

@@ -34,6 +34,10 @@ func NewDefaultConfig() *Config {
 
 
 // LoadConfigFromEnv loads configuration from the global config package
 // LoadConfigFromEnv loads configuration from the global config package
 func LoadConfigFromEnv(c *Config) (*Config, error) {
 func LoadConfigFromEnv(c *Config) (*Config, error) {
+	if c == nil {
+		c = NewDefaultConfig()
+	}
+
 	c.Username = config.SwiftUsername
 	c.Username = config.SwiftUsername
 	c.APIKey = config.SwiftAPIKey
 	c.APIKey = config.SwiftAPIKey
 	c.AuthURL = config.SwiftAuthURL
 	c.AuthURL = config.SwiftAuthURL