chore(repo): import lab2 sources as lab3 baseline

This commit is contained in:
Голиков Maximillian Антон
2026-10-06 18:59:51 +03:00
parent 0ab3a341aa
commit a7ba75be4b
53 changed files with 3244 additions and 2 deletions
@@ -0,0 +1,38 @@
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
"high-loud-apps-labs/internal/domain"
"high-loud-apps-labs/internal/httpapi/dto"
)
func Auth(users domain.UserRepository) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.GetHeader("X-User-Id")
if id == "" {
abortUnauthorized(c, "требуется X-User-Id")
return
}
u, err := users.GetByID(c.Request.Context(), id)
if err != nil {
abortUnauthorized(c, "неизвестный пользователь")
return
}
c.Set(CurrentUserKey, u)
c.Next()
}
}
func abortUnauthorized(c *gin.Context, msg string) {
reqID := GetRequestID(c)
body := dto.ErrorFromDomain(
&domain.UnauthorizedError{Reason: msg},
reqID,
)
c.AbortWithStatusJSON(http.StatusUnauthorized, body)
}