mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 01:08:47 +02:00
refactor(api): disable caching completely
This commit is contained in:
@@ -2,10 +2,10 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"reflect"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gin-gonic/gin/codec/json"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
apiV1 "github.com/yusing/godoxy/internal/api/v1"
|
apiV1 "github.com/yusing/godoxy/internal/api/v1"
|
||||||
@@ -45,6 +45,9 @@ func NewHandler() *gin.Engine {
|
|||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(ErrorHandler())
|
r.Use(ErrorHandler())
|
||||||
r.Use(ErrorLoggingMiddleware())
|
r.Use(ErrorLoggingMiddleware())
|
||||||
|
r.Use(NoCache())
|
||||||
|
|
||||||
|
log.Debug().Msg("gin codec json.API: " + reflect.TypeOf(json.API).Name())
|
||||||
|
|
||||||
r.GET("/api/v1/version", apiV1.Version)
|
r.GET("/api/v1/version", apiV1.Version)
|
||||||
|
|
||||||
@@ -69,7 +72,7 @@ func NewHandler() *gin.Engine {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
// enable cache for favicon
|
// enable cache for favicon
|
||||||
v1.GET("/favicon", apiV1.FavIcon).Use(Cache(time.Hour * 24))
|
v1.GET("/favicon", apiV1.FavIcon)
|
||||||
v1.GET("/health", apiV1.Health)
|
v1.GET("/health", apiV1.Health)
|
||||||
v1.GET("/icons", apiV1.Icons)
|
v1.GET("/icons", apiV1.Icons)
|
||||||
v1.POST("/reload", apiV1.Reload)
|
v1.POST("/reload", apiV1.Reload)
|
||||||
@@ -140,15 +143,13 @@ func NewHandler() *gin.Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable cache by default
|
|
||||||
r.Use(NoCache())
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func NoCache() gin.HandlerFunc {
|
func NoCache() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// skip cache if Cache-Control header is set or if caching is explicitly enabled
|
// skip cache if Cache-Control header is set
|
||||||
if !c.GetBool("cache_enabled") && c.Writer.Header().Get("Cache-Control") == "" {
|
if c.Writer.Header().Get("Cache-Control") == "" {
|
||||||
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
|
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||||
c.Header("Pragma", "no-cache")
|
c.Header("Pragma", "no-cache")
|
||||||
c.Header("Expires", "0")
|
c.Header("Expires", "0")
|
||||||
@@ -157,20 +158,6 @@ func NoCache() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cache(duration time.Duration) gin.HandlerFunc {
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
// Signal to NoCache middleware that caching is intended
|
|
||||||
c.Set("cache_enabled", true)
|
|
||||||
// skip cache if Cache-Control header is set
|
|
||||||
if c.Writer.Header().Get("Cache-Control") == "" {
|
|
||||||
c.Header("Cache-Control", "public, max-age="+strconv.FormatFloat(duration.Seconds(), 'f', 0, 64)+", immutable")
|
|
||||||
c.Header("Pragma", "public")
|
|
||||||
c.Header("Expires", time.Now().Add(duration).Format(time.RFC1123))
|
|
||||||
}
|
|
||||||
c.Next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func AuthMiddleware() gin.HandlerFunc {
|
func AuthMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
err := auth.GetDefaultAuth().CheckToken(c.Request)
|
err := auth.GetDefaultAuth().CheckToken(c.Request)
|
||||||
|
|||||||
Reference in New Issue
Block a user