2024-12-14 12:26:57 +07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-12-22 12:45:08 +07:00
|
|
|
"github.com/axzilla/templui/internal/config"
|
2025-02-08 13:52:30 +01:00
|
|
|
"github.com/axzilla/templui/internal/utils"
|
2024-12-14 12:26:57 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func WithPreviewCheck(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
isPreview := strings.HasPrefix(r.Host, "preview.")
|
|
|
|
ctx := context.WithValue(r.Context(), config.PreviewContextKey, isPreview)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
2024-12-29 15:24:09 +07:00
|
|
|
|
|
|
|
func CacheControlMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache, no-store, private")
|
|
|
|
w.Header().Set("Pragma", "no-cache")
|
|
|
|
w.Header().Set("Expires", "0")
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2025-02-08 13:52:30 +01:00
|
|
|
|
|
|
|
// WithURLPathValue adds the current URL's path to the context.
|
|
|
|
func WithURLPathValue(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.WithValue(
|
|
|
|
r.Context(),
|
|
|
|
utils.CtxURLPathValueKey,
|
|
|
|
r.URL.Path,
|
|
|
|
)
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|