Commit eaec14c
Eric Bower
·
2026-04-20 19:53:13 -0400 EDT
parent 54c886f
refactor(httpcache): ignore routes should use no-store
2 files changed,
+15,
-18
+8,
-2
| ... | ... | @@ -123,7 +123,6 @@ func NewPgsHttpCache(cfg *PgsConfig, upstream http.Handler) *httpcache.HttpCache | |
| 123 | 123 | TxtPrefix: cfg.TxtPrefix, | |
| 124 | 124 | }, | |
| 125 | 125 | CacheMetrics: metrics, | |
| 126 | - | IgnoreRoutes: []string{"/check", "/_metrics"}, | |
| 127 | 126 | } | |
| 128 | 127 | httpCache.Logger.Info("httpcache initiated", "ttl", httpCache.Ttl, "storage", "lru") | |
| 129 | 128 | return httpCache |
| ... | ... | @@ -212,7 +211,11 @@ func (web *WebRouter) initRouters() { | |
| 212 | 211 | // root domain | |
| 213 | 212 | rootRouter := http.NewServeMux() | |
| 214 | 213 | rootRouter.HandleFunc("GET /check", web.checkHandler) | |
| 215 | - | rootRouter.HandleFunc("GET /_metrics", promhttp.Handler().ServeHTTP) | |
| 214 | + | rootRouter.HandleFunc("GET /_metrics", func(w http.ResponseWriter, r *http.Request) { | |
| 215 | + | // we do *not* want to cache this handler | |
| 216 | + | w.Header().Set("cache-control", "no-store") | |
| 217 | + | promhttp.Handler().ServeHTTP(w, r) | |
| 218 | + | }) | |
| 216 | 219 | rootRouter.Handle("GET /main.css", web.serveFile("main.css", "text/css")) | |
| 217 | 220 | rootRouter.Handle("GET /favicon-16x16.png", web.serveFile("favicon-16x16.png", "image/png")) | |
| 218 | 221 | rootRouter.Handle("GET /favicon.ico", web.serveFile("favicon.ico", "image/x-icon")) |
| ... | ... | @@ -316,6 +319,9 @@ func (web *WebRouter) checkHandler(w http.ResponseWriter, r *http.Request) { | |
| 316 | 319 | hostDomain := r.URL.Query().Get("domain") | |
| 317 | 320 | appDomain := strings.Split(cfg.Domain, ":")[0] | |
| 318 | 321 | ||
| 322 | + | // we do *not* want to cache this handler | |
| 323 | + | w.Header().Set("cache-control", "no-store") | |
| 324 | + | ||
| 319 | 325 | if !strings.Contains(hostDomain, appDomain) { | |
| 320 | 326 | subdomain := router.GetCustomDomain(hostDomain, cfg.TxtPrefix) | |
| 321 | 327 | props, err := router.GetProjectFromSubdomain(subdomain) |
+7,
-16
| ... | ... | @@ -45,11 +45,10 @@ func (p *DefaultCacheMetrics) AddUpstreamRequest() {} | |
| 45 | 45 | type HttpCache struct { | |
| 46 | 46 | CacheKey | |
| 47 | 47 | CacheMetrics | |
| 48 | - | Ttl time.Duration | |
| 49 | - | Upstream http.Handler | |
| 50 | - | Cache Cacher | |
| 51 | - | Logger *slog.Logger | |
| 52 | - | IgnoreRoutes []string | |
| 48 | + | Ttl time.Duration | |
| 49 | + | Upstream http.Handler | |
| 50 | + | Cache Cacher | |
| 51 | + | Logger *slog.Logger | |
| 53 | 52 | } | |
| 54 | 53 | ||
| 55 | 54 | func NewHttpCache(log *slog.Logger, upstream http.Handler) *HttpCache { |
| ... | ... | @@ -73,14 +72,6 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 73 | 72 | return | |
| 74 | 73 | } | |
| 75 | 74 | ||
| 76 | - | reqUri := r.URL.Path | |
| 77 | - | for _, uri := range c.IgnoreRoutes { | |
| 78 | - | if uri == reqUri { | |
| 79 | - | c.Upstream.ServeHTTP(w, r) | |
| 80 | - | return | |
| 81 | - | } | |
| 82 | - | } | |
| 83 | - | ||
| 84 | 75 | cacheKey := c.GetCacheKey(r) | |
| 85 | 76 | log := c.Logger.With("cache_key", cacheKey) | |
| 86 | 77 |
| ... | ... | @@ -155,7 +146,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 155 | 146 | } | |
| 156 | 147 | cacheValue.Header[key] = values | |
| 157 | 148 | } | |
| 158 | - | // Revalidation refreshes the entry — reset CreatedAt so it's fresh again. | |
| 149 | + | // Revalidation refreshes the entry -- reset CreatedAt so it's fresh again. | |
| 159 | 150 | cacheValue.CreatedAt = time.Now() | |
| 160 | 151 | enc, _ := json.Marshal(cacheValue) | |
| 161 | 152 | c.Cache.Remove(cacheKey) |
| ... | ... | @@ -163,7 +154,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 163 | 154 | c.AddCacheItem(float64(len(enc))) | |
| 164 | 155 | ||
| 165 | 156 | if clientConditional { | |
| 166 | - | // Client sent conditional headers — re-evaluate against the | |
| 157 | + | // Client sent conditional headers -- re-evaluate against the | |
| 167 | 158 | // updated cached entry and return 304 if it still matches. | |
| 168 | 159 | r.Header.Set("If-None-Match", clientIfNoneMatch) | |
| 169 | 160 | r.Header.Set("If-Modified-Since", clientIfModifiedSince) |
| ... | ... | @@ -184,7 +175,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| 184 | 175 | } | |
| 185 | 176 | } | |
| 186 | 177 | ||
| 187 | - | // Client request was unconditional (or conditional but no longer matches) — | |
| 178 | + | // Client request was unconditional (or conditional but no longer matches) | |
| 188 | 179 | // serve the full cached response. | |
| 189 | 180 | serveCache(w, c.Ttl, cacheKey, &cacheValue) | |
| 190 | 181 | return |