diff options
Diffstat (limited to 'pkg/handler')
| -rw-r--r-- | pkg/handler/router.go | 4 | ||||
| -rw-r--r-- | pkg/handler/static/handler.go | 22 |
2 files changed, 19 insertions, 7 deletions
diff --git a/pkg/handler/router.go b/pkg/handler/router.go index bc81350..cb5d6f5 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -1,6 +1,7 @@ package handler import ( + "fmt" "net/http" serverconfig "git.gabrielgio.me/cerrado/pkg/config" @@ -10,6 +11,7 @@ import ( "git.gabrielgio.me/cerrado/pkg/handler/git" "git.gabrielgio.me/cerrado/pkg/handler/static" "git.gabrielgio.me/cerrado/pkg/service" + "git.gabrielgio.me/cerrado/templates" ) // Mount handler gets the requires service and repository to build the handlers @@ -53,7 +55,7 @@ func MountHandler( } mux.HandleFunc("/static/{file}", staticHandler) - mux.HandleFunc("/static/theme", cssStaticHandler) + mux.HandleFunc(fmt.Sprintf("/static/theme%s.css", templates.Slug), cssStaticHandler) // add slug so css file can be cached forever. mux.HandleFunc("/{name}/about/{$}", gitHandler.About) mux.HandleFunc("/{name}", gitHandler.Multiplex) mux.HandleFunc("/{name}/{rest...}", gitHandler.Multiplex) diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go index 779c786..6cc884e 100644 --- a/pkg/handler/static/handler.go +++ b/pkg/handler/static/handler.go @@ -1,6 +1,7 @@ package static import ( + "bytes" "fmt" "io" "io/fs" @@ -45,23 +46,32 @@ func ServeStaticCSSHandler(lightTheme, darkTheme string) (ext.ErrorRequestHandle return func(w http.ResponseWriter, r *ext.Request) error { ext.SetMIME(w, "text/css") + w.Header().Add("Cache-Control", "max-age=31536000") + + // use buffer so this function can fail before writing to http.ResponseWriter + var buffer bytes.Buffer var style *chroma.Style style = darkStyle - w.Write([]byte("[data-bs-theme=\"dark\"] {\n")) - err := formatter.WriteCSS(&ws{w}, style) + buffer.Write([]byte("[data-bs-theme=\"dark\"] {\n")) + err := formatter.WriteCSS(&ws{&buffer}, style) if err != nil { return err } - w.Write([]byte("}\n")) + buffer.Write([]byte("}\n")) style = lightStyle - w.Write([]byte("[data-bs-theme=\"light\"] {\n")) - err = formatter.WriteCSS(&ws{w}, style) + buffer.Write([]byte("[data-bs-theme=\"light\"] {\n")) + err = formatter.WriteCSS(&ws{&buffer}, style) + if err != nil { + return err + } + buffer.Write([]byte("}")) + + _, err = io.Copy(w, &buffer) if err != nil { return err } - w.Write([]byte("\n}")) return nil }, nil |
