Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jul 8, 2023
1 parent dae58ca commit d28a8aa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/PuerkitoBio/purell v1.2.0
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-playground/assert/v2 v2.2.0
github.com/go-playground/validator/v10 v10.14.1
Expand All @@ -21,8 +22,6 @@ require (
golang.org/x/net v0.12.0
)

require github.com/bmatcuk/doublestar/v4 v4.6.0

require (
atomicgo.dev/cursor v0.1.2 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/cache/cacheable_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (w *CacheableResponseWriter) WriteHeader(statusCode int) {

func (w *CacheableResponseWriter) GetCachedResponse() *CachedResponse {
header := w.original.Header().Clone()
clearHeader(header)
cleanupHeader(header)

return &CachedResponse{
Code: w.code,
Expand All @@ -59,6 +59,6 @@ func (w *CacheableResponseWriter) GetCachedResponse() *CachedResponse {
}
}

func clearHeader(header http.Header) {
func cleanupHeader(header http.Header) {
header.Del(headers.ContentLength)
}
40 changes: 22 additions & 18 deletions internal/handler/cache/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/url"
"sort"
"strings"
"time"

"github.com/bmatcuk/doublestar/v4"
"github.com/evg4b/uncors/internal/config"
Expand Down Expand Up @@ -37,28 +36,32 @@ func NewMiddleware(options ...MiddlewareOption) *Middleware {

func (m *Middleware) Wrap(next contracts.Handler) contracts.Handler {
return contracts.HandlerFunc(func(writer contracts.ResponseWriter, request *contracts.Request) {
if !m.cacheableRequest(request) {
if !m.isCacheableRequest(request) {
next.ServeHTTP(writer, request)

return
}

cacheKey := m.extractKey(request.URL)
if cachedResponse := m.getCachedResponse(cacheKey); cachedResponse != nil {
m.writeCachedResponse(writer, cachedResponse)
m.cacheRequest(writer, request, next)
})
}

m.logger.PrintResponse(request, writer.StatusCode())
func (m *Middleware) cacheRequest(writer contracts.ResponseWriter, request *contracts.Request, next contracts.Handler) {
cacheKey := m.extractCacheKey(request.URL)
if cachedResponse := m.getCachedResponse(cacheKey); cachedResponse != nil {
m.writeCachedResponse(writer, cachedResponse)

return
}
m.logger.PrintResponse(request, writer.StatusCode())

cacheableWriter := NewCacheableWriter(writer)
next.ServeHTTP(cacheableWriter, request)
if helpers.Is2xxCode(cacheableWriter.StatusCode()) {
response := cacheableWriter.GetCachedResponse()
m.storage.Set(cacheKey, response, time.Hour)
}
})
return
}

cacheableWriter := NewCacheableWriter(writer)
next.ServeHTTP(cacheableWriter, request)
if helpers.Is2xxCode(cacheableWriter.StatusCode()) {
response := cacheableWriter.GetCachedResponse()
m.storage.SetDefault(cacheKey, response)
}
}

func (m *Middleware) writeCachedResponse(writer contracts.ResponseWriter, cachedResponse *CachedResponse) {
Expand All @@ -77,7 +80,7 @@ func (m *Middleware) writeCachedResponse(writer contracts.ResponseWriter, cached
}
}

func (m *Middleware) cacheableRequest(request *contracts.Request) bool {
func (m *Middleware) isCacheableRequest(request *contracts.Request) bool {
return lo.Contains(m.methods, request.Method) && lo.ContainsBy(m.pathGlobs, func(pattern string) bool {
ok, err := doublestar.PathMatch(pattern, request.URL.Path)
if err != nil {
Expand All @@ -88,12 +91,13 @@ func (m *Middleware) cacheableRequest(request *contracts.Request) bool {
})
}

func (m *Middleware) extractKey(url *url.URL) string {
func (m *Middleware) extractCacheKey(url *url.URL) string {
values := url.Query()
items := make([]string, 0, len(values))
for key, value := range values {
sort.Strings(value)
items = append(items, key+"="+strings.Join(value, ","))
valuesKey := key + "=" + strings.Join(value, ",")
items = append(items, valuesKey)
}

sort.Strings(items)
Expand Down

0 comments on commit d28a8aa

Please sign in to comment.