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 a8d2e5a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
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 a8d2e5a

Please sign in to comment.