add static cache-control

This commit is contained in:
aichy126 2023-04-24 18:23:39 +08:00 committed by LinkinStars
parent 3f4ed4527a
commit 779bd86e1d
2 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1,15 @@
package middleware
import (
"strings"
"github.com/gin-gonic/gin"
)
func HeadersByRequestURI() gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.RequestURI, "/static/") {
c.Header("cache-control", "public, max-age=31536000")
}
}
}

View File

@ -36,7 +36,7 @@ func NewHTTPServer(debug bool,
html, _ := fs.Sub(ui.Template, "template")
htmlTemplate := template.Must(template.New("").Funcs(funcMap).ParseFS(html, "*"))
r.SetHTMLTemplate(htmlTemplate)
r.Use(middleware.HeadersByRequestURI())
viewRouter.Register(r)
rootGroup := r.Group("")