Skip to content

Commit ab9f018

Browse files
committed
refactor: apply traffic control on all cacheable requests by default
1 parent 9fa09d6 commit ab9f018

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

handler/dynamic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
func DynamicMiddleware(next http.Handler) http.Handler {
99
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10+
handler := TrafficMiddleware(next)
1011
for dynamicCacheClient != nil {
1112
if rangeInHeader := r.Header.Get(headerRange); rangeInHeader != "" {
1213
break
@@ -15,11 +16,10 @@ func DynamicMiddleware(next http.Handler) http.Handler {
1516
ctx := context.WithValue(context.Background(), cacheClientCtxKey, dynamicCacheClient)
1617
r = r.Clone(ctx)
1718
r.URL.Query().Set(headerToken, token)
18-
CacheMiddleware(next).ServeHTTP(w, r)
19-
return
19+
handler = CacheMiddleware(handler)
2020
}
2121
break
2222
}
23-
next.ServeHTTP(w, r)
23+
handler.ServeHTTP(w, r)
2424
})
2525
}

handler/static.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77

88
func StaticMiddleware(next http.Handler) http.Handler {
99
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10+
handler := TrafficMiddleware(next)
1011
if staticCacheClient != nil {
1112
ctx := context.WithValue(context.Background(), cacheClientCtxKey, staticCacheClient)
1213
r = r.Clone(ctx)
13-
CacheMiddleware(next).ServeHTTP(w, r)
14-
return
14+
handler = CacheMiddleware(handler)
1515
}
16-
next.ServeHTTP(w, r)
16+
handler.ServeHTTP(w, r)
1717
})
1818
}

handler/user.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import (
77

88
func UserMiddleware(next http.Handler) http.Handler {
99
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10+
handler := TrafficMiddleware(next)
1011
if userCacheClient != nil {
1112
if token := r.Header.Get(headerToken); token != "" {
1213
ctx := context.WithValue(context.Background(), cacheClientCtxKey, userCacheClient)
1314
r = r.Clone(ctx)
1415
r.URL.Query().Set(headerToken, token)
15-
CacheMiddleware(next).ServeHTTP(w, r)
16-
return
16+
handler = CacheMiddleware(handler)
1717
}
1818
}
19-
next.ServeHTTP(w, r)
19+
handler.ServeHTTP(w, r)
2020
})
2121
}

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@ func newRouter() http.Handler {
2525
refreshRouter.Path("/library/sections/{id}/refresh").HandlerFunc(handler.Handler)
2626

2727
staticRouter := r.Methods(http.MethodGet).Subrouter()
28-
staticRouter.Use(handler.TrafficMiddleware)
2928
staticRouter.Use(handler.StaticMiddleware)
3029
staticRouter.Path("/library/metadata/{key}/art/{id}").HandlerFunc(handler.Handler)
3130
staticRouter.Path("/library/metadata/{key}/thumb/{id}").HandlerFunc(handler.Handler)
3231
staticRouter.Path("/photo/:/transcode").HandlerFunc(handler.Handler)
3332

3433
userRouter := r.Methods(http.MethodGet).Subrouter()
35-
userRouter.Use(handler.TrafficMiddleware)
3634
userRouter.Use(handler.UserMiddleware)
3735
userRouter.PathPrefix("/library/collections/").HandlerFunc(handler.Handler)
3836
userRouter.PathPrefix("/library/metadata/").HandlerFunc(handler.Handler)
3937
userRouter.PathPrefix("/library/sections/").HandlerFunc(handler.Handler)
4038

4139
dynamicRouter := r.Methods(http.MethodGet).Subrouter()
42-
dynamicRouter.Use(handler.TrafficMiddleware)
4340
dynamicRouter.Use(handler.DynamicMiddleware)
4441
dynamicRouter.PathPrefix("/").HandlerFunc(handler.Handler)
4542

0 commit comments

Comments
 (0)