wp-go/app/middleware/staticFileCache.go

34 lines
655 B
Go
Raw Permalink Normal View History

2022-09-14 05:28:31 +00:00
package middleware
import (
2023-02-05 13:29:09 +00:00
"fmt"
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/config"
2022-09-14 05:28:31 +00:00
"github.com/gin-gonic/gin"
"net/http"
"path/filepath"
2022-09-14 05:28:31 +00:00
"strings"
)
2023-02-05 13:29:09 +00:00
var path = map[string]struct{}{
2023-02-06 09:58:24 +00:00
"wp-includes": {},
2023-02-05 13:29:09 +00:00
"wp-content": {},
"favicon.ico": {},
}
2022-09-14 05:28:31 +00:00
func SetStaticFileCache(c *gin.Context) {
f := strings.Split(strings.TrimLeft(c.FullPath(), "/"), "/")
2023-02-05 13:29:09 +00:00
if _, ok := path[f[0]]; ok {
if ".php" == filepath.Ext(c.Request.URL.Path) {
c.Abort()
c.Status(http.StatusForbidden)
return
}
2023-02-05 13:29:09 +00:00
t := config.GetConfig().CacheTime.CacheControl
if t > 0 {
c.Header("Cache-Control", fmt.Sprintf("private, max-age=%d", int(t.Seconds())))
}
2022-09-14 05:28:31 +00:00
}
2023-02-05 13:29:09 +00:00
2022-09-14 13:30:59 +00:00
c.Next()
2022-09-14 05:28:31 +00:00
}