refactor(middleware): simplify buffer allocation in themed middleware

This commit is contained in:
yusing
2025-09-02 23:28:47 +08:00
parent ed39942d65
commit 8339c42470

View File

@@ -45,7 +45,9 @@ var (
var fontCSSTemplate = template.Must(template.New("fontCSS").Parse(fontCSS)) var fontCSSTemplate = template.Must(template.New("fontCSS").Parse(fontCSS))
const overAllocate = 256 func (m *themed) setup() {
m.m.setup()
}
func (m *themed) before(w http.ResponseWriter, req *http.Request) bool { func (m *themed) before(w http.ResponseWriter, req *http.Request) bool {
return m.m.before(w, req) return m.m.before(w, req)
@@ -58,9 +60,8 @@ func (m *themed) modifyResponse(resp *http.Response) error {
func (m *themed) finalize() error { func (m *themed) finalize() error {
m.m.Target = "body" m.m.Target = "body"
if m.FontURL != "" && m.FontFamily != "" { if m.FontURL != "" && m.FontFamily != "" {
buf := bytes.NewBuffer(bytePool.GetSized(len(fontCSS) + overAllocate)) buf := bytes.NewBuffer(nil)
buf.WriteString(`<style type="text/css">`) buf.WriteString(`<style type="text/css">`)
defer bytePool.Put(buf.Bytes())
err := fontCSSTemplate.Execute(buf, m) err := fontCSSTemplate.Execute(buf, m)
if err != nil { if err != nil {
return err return err