合并最新 main
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package adminhandler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/fs"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/apikey"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/clientip"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/gtin"
|
||||
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||
)
|
||||
@@ -32,6 +34,12 @@ type Handler struct {
|
||||
submitLimit *rateLimiter
|
||||
loginLimit *loginLimiter
|
||||
usage *ratelimit.Limiter
|
||||
cache cacheBumper
|
||||
clientIP *clientip.Resolver
|
||||
}
|
||||
|
||||
type cacheBumper interface {
|
||||
Bump(context.Context) error
|
||||
}
|
||||
|
||||
// New constructs an admin Handler. basePath is e.g. "/ping" (no trailing slash).
|
||||
@@ -44,6 +52,7 @@ func New(store *adminstore.Store, authn *auth.Authenticator, basePath string, sp
|
||||
spa: spa,
|
||||
submitLimit: newRateLimiter(5, 10*time.Minute),
|
||||
loginLimit: newLoginLimiter(10, 20, 15*time.Minute),
|
||||
clientIP: clientip.Default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,11 +63,48 @@ func (h *Handler) WithUsage(l *ratelimit.Limiter) *Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
// WithCache attaches a cache epoch invalidator for successful admin writes.
|
||||
func (h *Handler) WithCache(c cacheBumper) *Handler {
|
||||
h.cache = c
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Handler) invalidateCache(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if h.cache == nil || r.Method == http.MethodGet || r.Method == http.MethodHead {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
next.ServeHTTP(ww, r)
|
||||
status := ww.Status()
|
||||
if status == 0 {
|
||||
status = http.StatusOK
|
||||
}
|
||||
if status < http.StatusOK || status >= http.StatusMultipleChoices {
|
||||
return
|
||||
}
|
||||
if err := h.cache.Bump(context.WithoutCancel(r.Context())); err != nil {
|
||||
log.Printf("admin cache invalidation failed: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// WithClientIP sets the trusted-proxy resolver used to normalize request IPs.
|
||||
func (h *Handler) WithClientIP(resolver *clientip.Resolver) *Handler {
|
||||
if resolver == nil {
|
||||
resolver = clientip.Default()
|
||||
}
|
||||
h.clientIP = resolver
|
||||
return h
|
||||
}
|
||||
|
||||
// Router builds the HTTP handler.
|
||||
func (h *Handler) Router() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(h.clientIP.Middleware)
|
||||
r.Use(middleware.Recoverer)
|
||||
|
||||
r.Route(h.basePath, func(r chi.Router) {
|
||||
@@ -69,6 +115,7 @@ func (h *Handler) Router() http.Handler {
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(h.authn.Middleware)
|
||||
r.Use(h.invalidateCache)
|
||||
r.Post("/api/logout", h.Logout)
|
||||
r.Get("/api/me", h.Me)
|
||||
r.Get("/api/stats", h.Stats)
|
||||
@@ -802,14 +849,8 @@ func (h *Handler) RejectSubmission(w http.ResponseWriter, r *http.Request) {
|
||||
// ---------- helpers ----------
|
||||
|
||||
func realIP(r *http.Request) string {
|
||||
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
||||
if i := strings.IndexByte(ip, ','); i >= 0 {
|
||||
return strings.TrimSpace(ip[:i])
|
||||
}
|
||||
return strings.TrimSpace(ip)
|
||||
}
|
||||
if ip := r.Header.Get("X-Real-IP"); ip != "" {
|
||||
return ip
|
||||
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||
return host
|
||||
}
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user