lab 3 done
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
requestIDKey = "request_id"
|
||||
userKey = "current_user"
|
||||
)
|
||||
|
||||
func (h *Chat) requestIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
requestID := uuid.NewString()
|
||||
c.Set(requestIDKey, requestID)
|
||||
c.Response().Header().Set("X-Request-Id", requestID)
|
||||
c.Response().Header().Set("X-Instance-Id", h.instanceID)
|
||||
if h.draining.Load() && c.Request().URL.Path != "/health/live" {
|
||||
return h.writeError(c, http.StatusServiceUnavailable, "unavailable", "Реплика завершает работу")
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Chat) authenticate(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
user, err := h.useCase.Authenticate(c.Request().Context(), bearerToken(c))
|
||||
if err != nil {
|
||||
return h.useCaseError(c, err)
|
||||
}
|
||||
c.Set(userKey, user)
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
func authenticatedUser(c echo.Context) domain.User {
|
||||
return c.Get(userKey).(domain.User)
|
||||
}
|
||||
Reference in New Issue
Block a user