| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package handlers |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/tenseleyFlow/shithub/internal/repos/highlight" |
| 10 | ) |
| 11 | |
| 12 | // chromaCSSHandler serves the runtime-generated Chroma stylesheet at |
| 13 | // /static/css/chroma.css. The CSS is theme-derived (Chroma's `github` |
| 14 | // style) and immutable for the process lifetime, so we cache it once. |
| 15 | // |
| 16 | // Long-cache headers are safe because changing the theme would also |
| 17 | // require a binary upgrade — the operator's restart invalidates the |
| 18 | // upstream cache. |
| 19 | func chromaCSSHandler() http.HandlerFunc { |
| 20 | var ( |
| 21 | once sync.Once |
| 22 | body []byte |
| 23 | ) |
| 24 | return func(w http.ResponseWriter, _ *http.Request) { |
| 25 | once.Do(func() { body = []byte(highlight.CSS()) }) |
| 26 | w.Header().Set("Content-Type", "text/css; charset=utf-8") |
| 27 | w.Header().Set("Cache-Control", "public, max-age=86400, must-revalidate") |
| 28 | _, _ = w.Write(body) |
| 29 | } |
| 30 | } |
| 31 |