repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
eaec14c
parent
54c886f
author
Eric Bower
date
2026-04-20 19:53:13 -0400 EDT
refactor(httpcache): ignore routes should use no-store
2 files changed,  +15, -18
M pkg/apps/pgs/web.go
+8, -2
 1@@ -123,7 +123,6 @@ func NewPgsHttpCache(cfg *PgsConfig, upstream http.Handler) *httpcache.HttpCache
 2 			TxtPrefix: cfg.TxtPrefix,
 3 		},
 4 		CacheMetrics: metrics,
 5-		IgnoreRoutes: []string{"/check", "/_metrics"},
 6 	}
 7 	httpCache.Logger.Info("httpcache initiated", "ttl", httpCache.Ttl, "storage", "lru")
 8 	return httpCache
 9@@ -212,7 +211,11 @@ func (web *WebRouter) initRouters() {
10 	// root domain
11 	rootRouter := http.NewServeMux()
12 	rootRouter.HandleFunc("GET /check", web.checkHandler)
13-	rootRouter.HandleFunc("GET /_metrics", promhttp.Handler().ServeHTTP)
14+	rootRouter.HandleFunc("GET /_metrics", func(w http.ResponseWriter, r *http.Request) {
15+		// we do *not* want to cache this handler
16+		w.Header().Set("cache-control", "no-store")
17+		promhttp.Handler().ServeHTTP(w, r)
18+	})
19 	rootRouter.Handle("GET /main.css", web.serveFile("main.css", "text/css"))
20 	rootRouter.Handle("GET /favicon-16x16.png", web.serveFile("favicon-16x16.png", "image/png"))
21 	rootRouter.Handle("GET /favicon.ico", web.serveFile("favicon.ico", "image/x-icon"))
22@@ -316,6 +319,9 @@ func (web *WebRouter) checkHandler(w http.ResponseWriter, r *http.Request) {
23 	hostDomain := r.URL.Query().Get("domain")
24 	appDomain := strings.Split(cfg.Domain, ":")[0]
25 
26+	// we do *not* want to cache this handler
27+	w.Header().Set("cache-control", "no-store")
28+
29 	if !strings.Contains(hostDomain, appDomain) {
30 		subdomain := router.GetCustomDomain(hostDomain, cfg.TxtPrefix)
31 		props, err := router.GetProjectFromSubdomain(subdomain)
M pkg/httpcache/serve.go
+7, -16
 1@@ -45,11 +45,10 @@ func (p *DefaultCacheMetrics) AddUpstreamRequest()  {}
 2 type HttpCache struct {
 3 	CacheKey
 4 	CacheMetrics
 5-	Ttl          time.Duration
 6-	Upstream     http.Handler
 7-	Cache        Cacher
 8-	Logger       *slog.Logger
 9-	IgnoreRoutes []string
10+	Ttl      time.Duration
11+	Upstream http.Handler
12+	Cache    Cacher
13+	Logger   *slog.Logger
14 }
15 
16 func NewHttpCache(log *slog.Logger, upstream http.Handler) *HttpCache {
17@@ -73,14 +72,6 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
18 		return
19 	}
20 
21-	reqUri := r.URL.Path
22-	for _, uri := range c.IgnoreRoutes {
23-		if uri == reqUri {
24-			c.Upstream.ServeHTTP(w, r)
25-			return
26-		}
27-	}
28-
29 	cacheKey := c.GetCacheKey(r)
30 	log := c.Logger.With("cache_key", cacheKey)
31 
32@@ -155,7 +146,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
33 			}
34 			cacheValue.Header[key] = values
35 		}
36-		// Revalidation refreshes the entry — reset CreatedAt so it's fresh again.
37+		// Revalidation refreshes the entry -- reset CreatedAt so it's fresh again.
38 		cacheValue.CreatedAt = time.Now()
39 		enc, _ := json.Marshal(cacheValue)
40 		c.Cache.Remove(cacheKey)
41@@ -163,7 +154,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
42 		c.AddCacheItem(float64(len(enc)))
43 
44 		if clientConditional {
45-			// Client sent conditional headers — re-evaluate against the
46+			// Client sent conditional headers -- re-evaluate against the
47 			// updated cached entry and return 304 if it still matches.
48 			r.Header.Set("If-None-Match", clientIfNoneMatch)
49 			r.Header.Set("If-Modified-Since", clientIfModifiedSince)
50@@ -184,7 +175,7 @@ func (c *HttpCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
51 			}
52 		}
53 
54-		// Client request was unconditional (or conditional but no longer matches) —
55+		// Client request was unconditional (or conditional but no longer matches)
56 		// serve the full cached response.
57 		serveCache(w, c.Ttl, cacheKey, &cacheValue)
58 		return