-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added custom handler type * Added tests * Renaming * Added base middelaware structure * Added math matching * Added base tests for cache middleware * CacheableWriter did not save Content-Lenght * Added tests for cache middlerware * Removed config * Ignored default configuration for repo * Updated AssertIsDefined function * Inject cache middleware factory from main function * Provided cache configuration * Added MiddlewareHandler interface * Small refactoring * Updated response writing * Added default configs * Added methods to cache midelware * Cached responses only with 2xx status code * Code refactoring
- Loading branch information
Showing
71 changed files
with
1,788 additions
and
903 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dist/ | |
uncors | ||
.idea | ||
node_modules | ||
.uncors.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package config | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type CacheGlobs []string | ||
|
||
func (globs CacheGlobs) Clone() CacheGlobs { | ||
if globs == nil { | ||
return nil | ||
} | ||
|
||
cacheGlobs := make(CacheGlobs, 0, len(globs)) | ||
cacheGlobs = append(cacheGlobs, globs...) | ||
|
||
return cacheGlobs | ||
} | ||
|
||
type CacheConfig struct { | ||
ExpirationTime time.Duration `mapstructure:"expiration-time"` | ||
ClearTime time.Duration `mapstructure:"clear-time"` | ||
Methods []string `mapstructure:"methods"` | ||
} | ||
|
||
func (config *CacheConfig) Clone() *CacheConfig { | ||
var methods []string | ||
if config.Methods != nil { | ||
methods = append(methods, config.Methods...) | ||
} | ||
|
||
return &CacheConfig{ | ||
ExpirationTime: config.ExpirationTime, | ||
ClearTime: config.ClearTime, | ||
Methods: methods, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package config_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/evg4b/uncors/internal/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCacheGlobsClone(t *testing.T) { | ||
globs := config.CacheGlobs{ | ||
"/api/**", | ||
"/constants", | ||
"/translations", | ||
"/**/*.js", | ||
} | ||
|
||
cacheGlobs := globs.Clone() | ||
|
||
t.Run("not same", func(t *testing.T) { | ||
assert.NotSame(t, globs, cacheGlobs) | ||
}) | ||
|
||
t.Run("equals values", func(t *testing.T) { | ||
assert.EqualValues(t, globs, cacheGlobs) | ||
}) | ||
} | ||
|
||
func TestCacheConfigClone(t *testing.T) { | ||
cacheConfig := &config.CacheConfig{ | ||
ExpirationTime: 5 * time.Minute, | ||
ClearTime: 30 * time.Second, | ||
Methods: []string{http.MethodGet, http.MethodPost}, | ||
} | ||
|
||
clonedCacheConfig := cacheConfig.Clone() | ||
|
||
t.Run("not same", func(t *testing.T) { | ||
assert.NotSame(t, cacheConfig, clonedCacheConfig) | ||
}) | ||
|
||
t.Run("equals values", func(t *testing.T) { | ||
assert.EqualValues(t, cacheConfig, clonedCacheConfig) | ||
}) | ||
|
||
t.Run("not same methods", func(t *testing.T) { | ||
assert.NotSame(t, cacheConfig.Methods, clonedCacheConfig.Methods) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package config | ||
|
||
import ( | ||
"net/http" | ||
"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) | ||
instance.SetDefault("cache-config.methods", []string{http.MethodGet}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.