@@ -40,10 +40,10 @@ import (
40
40
backend "github.com/dadav/gorge/internal/v3/backend"
41
41
"github.com/dadav/gorge/internal/v3/ui"
42
42
openapi "github.com/dadav/gorge/pkg/gen/v3/openapi"
43
+ "github.com/dadav/stampede"
43
44
"github.com/go-chi/chi/v5"
44
45
"github.com/go-chi/chi/v5/middleware"
45
46
"github.com/go-chi/cors"
46
- "github.com/go-chi/stampede"
47
47
"github.com/spf13/cobra"
48
48
"golang.org/x/sync/errgroup"
49
49
)
@@ -125,6 +125,16 @@ You can also enable the caching functionality to speed things up.`,
125
125
userService := v3 .NewUserOperationsApi ()
126
126
127
127
r := chi .NewRouter ()
128
+
129
+ // 0. Inject statistic middleware
130
+ x := customMiddleware .NewStatistics ()
131
+ r .Use (func (next http.Handler ) http.Handler {
132
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
133
+ ctx := context .WithValue (r .Context (), "stats" , x )
134
+ next .ServeHTTP (w , r .WithContext (ctx ))
135
+ })
136
+ })
137
+
128
138
// 1. Recoverer should be first to catch panics in all other middleware
129
139
r .Use (middleware .Recoverer )
130
140
// 2. RealIP should be early to ensure all other middleware sees the correct IP
@@ -140,8 +150,6 @@ You can also enable the caching functionality to speed things up.`,
140
150
// 4. RequireUserAgent should be early to ensure all other middleware sees the correct user agent
141
151
r .Use (customMiddleware .RequireUserAgent )
142
152
143
- x := customMiddleware .NewStatistics ()
144
-
145
153
if config .UI {
146
154
r .Group (func (r chi.Router ) {
147
155
r .HandleFunc ("/" , ui .IndexHandler )
@@ -168,10 +176,28 @@ You can also enable the caching functionality to speed things up.`,
168
176
return stampede .StringToHash (r .Method , requestURI , strings .ToLower (token ))
169
177
}
170
178
171
- cachedMiddleware := stampede .HandlerWithKey (
179
+ cbFunc := func (fromCache bool , w http.ResponseWriter , r * http.Request ) error {
180
+ x .Mutex .Lock ()
181
+ if fromCache {
182
+ log .Log .Debugf ("Cache hit for path: %s" , r .URL .Path )
183
+ x .TotalCacheHits ++
184
+ x .CacheHitsPerEndpoint [r .URL .Path ]++
185
+ w .Header ().Set ("X-Cache" , "Hit from gorge" )
186
+ } else {
187
+ log .Log .Debugf ("Cache miss for path: %s" , r .URL .Path )
188
+ x .TotalCacheMisses ++
189
+ x .CacheMissesPerEndpoint [r .URL .Path ]++
190
+ w .Header ().Set ("X-Cache" , "MISS from gorge" )
191
+ }
192
+ x .Mutex .Unlock ()
193
+ return nil
194
+ }
195
+
196
+ cachedMiddleware := stampede .HandlerWithKeyAndCb (
172
197
512 ,
173
198
time .Duration (config .CacheMaxAge )* time .Second ,
174
199
customKeyFunc ,
200
+ cbFunc ,
175
201
)
176
202
log .Log .Debugf ("Cache middleware configured with prefixes: %s" , config .CachePrefixes )
177
203
@@ -188,21 +214,7 @@ You can also enable the caching functionality to speed things up.`,
188
214
}
189
215
190
216
if shouldCache {
191
- wrapper := customMiddleware .NewResponseWrapper (w )
192
- // Set default cache status before serving
193
- // w.Header().Set("X-Cache", "MISS from gorge")
194
-
195
- cachedMiddleware (next ).ServeHTTP (wrapper , r )
196
-
197
- // Only override if it was served from cache
198
- // TODO: this is not working as expected
199
- if wrapper .WasWritten () {
200
- log .Log .Debugf ("Serving cached response for path: %s" , r .URL .Path )
201
- w .Header ().Set ("X-Cache" , "HIT from gorge" )
202
- } else {
203
- log .Log .Debugf ("Cache miss for path: %s" , r .URL .Path )
204
- w .Header ().Set ("X-Cache" , "MISS from gorge" )
205
- }
217
+ cachedMiddleware (next ).ServeHTTP (w , r )
206
218
} else {
207
219
next .ServeHTTP (w , r )
208
220
}
0 commit comments