Skip to content

Commit f3f74c8

Browse files
committed
fix: interface {} is nil, not *cache.Client
1 parent 1c157c9 commit f3f74c8

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

handler/cache.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ import (
88

99
func CacheMiddleware(next http.Handler) http.Handler {
1010
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
11-
cacheClient := r.Context().Value(cacheClientCtxKey).(*cache.Client)
12-
rangeInHeader := r.Header.Get(headerRange)
13-
if cacheClient != nil && rangeInHeader == "" {
14-
mu.RLock()
15-
defer mu.RUnlock()
11+
mu.RLock()
12+
defer mu.RUnlock()
1613

17-
cacheClient.Middleware(next).ServeHTTP(w, r)
18-
} else {
19-
next.ServeHTTP(w, r)
20-
}
14+
cacheClient := r.Context().Value(cacheClientCtxKey).(*cache.Client)
15+
cacheClient.Middleware(next).ServeHTTP(w, r)
2116
})
2217
}

handler/dynamic.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import (
77

88
func DynamicMiddleware(next http.Handler) http.Handler {
99
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10-
if dynamicCacheClient != nil {
10+
for dynamicCacheClient != nil {
11+
if rangeInHeader := r.Header.Get(headerRange); rangeInHeader != "" {
12+
break
13+
}
1114
if token := r.Header.Get(headerToken); token != "" {
1215
ctx := context.WithValue(context.Background(), cacheClientCtxKey, dynamicCacheClient)
1316
r = r.Clone(ctx)
1417
r.URL.Query().Set(headerToken, token)
18+
CacheMiddleware(next).ServeHTTP(w, r)
19+
return
1520
}
21+
break
1622
}
17-
CacheMiddleware(next).ServeHTTP(w, r)
23+
next.ServeHTTP(w, r)
1824
})
1925
}

handler/static.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ func StaticMiddleware(next http.Handler) http.Handler {
1010
if staticCacheClient != nil {
1111
ctx := context.WithValue(context.Background(), cacheClientCtxKey, staticCacheClient)
1212
r = r.Clone(ctx)
13+
CacheMiddleware(next).ServeHTTP(w, r)
14+
return
1315
}
14-
CacheMiddleware(next).ServeHTTP(w, r)
16+
next.ServeHTTP(w, r)
1517
})
1618
}

handler/user.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ func UserMiddleware(next http.Handler) http.Handler {
1212
ctx := context.WithValue(context.Background(), cacheClientCtxKey, userCacheClient)
1313
r = r.Clone(ctx)
1414
r.URL.Query().Set(headerToken, token)
15+
CacheMiddleware(next).ServeHTTP(w, r)
16+
return
1517
}
1618
}
17-
CacheMiddleware(next).ServeHTTP(w, r)
19+
next.ServeHTTP(w, r)
1820
})
1921
}

0 commit comments

Comments
 (0)