✨ improve image sharing function
This commit is contained in:
@@ -3,7 +3,10 @@ package middleware
|
||||
import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
"net/http"
|
||||
"schisandra-album-cloud-microservices/common/middleware"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
"schisandra-album-cloud-microservices/common/errors"
|
||||
"schisandra-album-cloud-microservices/common/xhttp"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NonceMiddleware struct {
|
||||
@@ -18,7 +21,25 @@ func NewNonceMiddleware(redisClient *redis.Client) *NonceMiddleware {
|
||||
|
||||
func (m *NonceMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
middleware.NonceMiddleware(w, r, m.RedisClient)
|
||||
nonce := r.Header.Get("X-Nonce")
|
||||
if nonce == "" {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New(http.StatusBadRequest, "bad request!"))
|
||||
return
|
||||
}
|
||||
if len(nonce) != 32 {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New(http.StatusBadRequest, "bad request!"))
|
||||
return
|
||||
}
|
||||
result := m.RedisClient.Get(r.Context(), constant.SystemApiNoncePrefix+nonce).Val()
|
||||
if result != "" {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New(http.StatusBadRequest, "bad request!"))
|
||||
return
|
||||
}
|
||||
err := m.RedisClient.Set(r.Context(), constant.SystemApiNoncePrefix+nonce, nonce, time.Minute*1).Err()
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New(http.StatusInternalServerError, "internal server error!"))
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user