Skip to content

Commit

Permalink
Added default configs
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jul 6, 2023
1 parent 3e41b7b commit 38865d0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func LoadConfiguration(viperInstance *viper.Viper, args []string) (*UncorsConfig
URLMappingHookFunc(),
))

setDefaultValues(viperInstance)
if err := viperInstance.Unmarshal(configuration, configOption); err != nil {
return nil, fmt.Errorf("filed parsing config: %w", err)
}
Expand Down
20 changes: 20 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config_test

import (
"testing"
"time"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/testing/testconstants"
Expand Down Expand Up @@ -47,6 +48,9 @@ debug: true
https-port: 8081
cert-file: /etc/certificates/cert-file.pem
key-file: /etc/certificates/key-file.key
cache-config:
expiration-time: 1h
clear-time: 30m
`
)

Expand Down Expand Up @@ -89,6 +93,10 @@ func TestLoadConfiguration(t *testing.T) {
HTTPPort: 80,
HTTPSPort: 443,
Mappings: config.Mappings{},
CacheConfig: config.CacheConfig{
ExpirationTime: config.DefaultExpirationTime,
ClearTime: config.DefaultClearTime,
},
},
},
{
Expand All @@ -100,6 +108,10 @@ func TestLoadConfiguration(t *testing.T) {
Mappings: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
},
CacheConfig: config.CacheConfig{
ExpirationTime: config.DefaultExpirationTime,
ClearTime: config.DefaultClearTime,
},
},
},
{
Expand Down Expand Up @@ -139,6 +151,10 @@ func TestLoadConfiguration(t *testing.T) {
HTTPSPort: 8081,
CertFile: testconstants.CertFilePath,
KeyFile: testconstants.KeyFilePath,
CacheConfig: config.CacheConfig{
ExpirationTime: time.Hour,
ClearTime: 30 * time.Minute,
},
},
},
{
Expand Down Expand Up @@ -186,6 +202,10 @@ func TestLoadConfiguration(t *testing.T) {
HTTPSPort: 8081,
CertFile: testconstants.CertFilePath,
KeyFile: testconstants.KeyFilePath,
CacheConfig: config.CacheConfig{
ExpirationTime: time.Hour,
ClearTime: 30 * time.Minute,
},
},
},
}
Expand Down
17 changes: 17 additions & 0 deletions internal/config/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import (
"time"

"github.com/spf13/viper"
)

const (
DefaultExpirationTime = 30 * time.Minute
DefaultClearTime = 30 * time.Minute
)

func setDefaultValues(instance *viper.Viper) {
instance.SetDefault("cache-config.expiration-time", DefaultExpirationTime)
instance.SetDefault("cache-config.clear-time", DefaultClearTime)
}
11 changes: 8 additions & 3 deletions internal/handler/uncors_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ func NewUncorsRequestHandler(options ...RequestHandlerOption) *RequestHandler {
handler.makeStaticRoutes(router, mapping.Statics, proxyHandler)
handler.makeMockedRoutes(router, mapping.Mocks)

cachePrefix := fmt.Sprintf("mapping_%d", index)
cacheMiddleware := handler.cacheMiddlewareFactory(cachePrefix, mapping.Cache)
setDefaultHandler(router, cacheMiddleware.Wrap(proxyHandler))
var defaultHandler contracts.Handler = proxyHandler
if len(mapping.Cache) > 0 {
cachePrefix := fmt.Sprintf("mapping_%d", index)
cacheMiddleware := handler.cacheMiddlewareFactory(cachePrefix, mapping.Cache)
defaultHandler = cacheMiddleware.Wrap(proxyHandler)
}

setDefaultHandler(router, defaultHandler)
}

setDefaultHandler(handler.router, contracts.HandlerFunc(func(writer contracts.ResponseWriter, _ *http.Request) {
Expand Down

0 comments on commit 38865d0

Please sign in to comment.