lab2: fix review — cache consistency, readiness, timeouts, uuid validation, tuple index
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-10 01:14:56 +03:00
parent a327909856
commit 40f260612f
21 changed files with 185 additions and 189 deletions
+26 -4
View File
@@ -1,22 +1,44 @@
package http
import (
"context"
"net/http"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"github.com/charawein/mtusi2026lab2/internal/service"
"github.com/charawein/mtusi2026lab2/internal/transport/httputil"
)
type HealthHandler struct{}
func NewHealthHandler() *HealthHandler {
return &HealthHandler{}
type HealthHandler struct {
pool *pgxpool.Pool
redis *redis.Client
}
func NewHealthHandler(pool *pgxpool.Pool, redisClient *redis.Client) *HealthHandler {
return &HealthHandler{pool: pool, redis: redisClient}
}
// Live проверяет только жизнь процесса.
func (h *HealthHandler) Live(w http.ResponseWriter, r *http.Request) {
httputil.WriteJSON(w, r, http.StatusOK, healthResponse{Status: "ok"})
}
// Ready проверяет доступность PostgreSQL и Redis.
func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if err := h.pool.Ping(ctx); err != nil {
httputil.WriteError(w, r, service.ErrUnavailable)
return
}
if err := h.redis.Ping(ctx).Err(); err != nil {
httputil.WriteError(w, r, service.ErrUnavailable)
return
}
httputil.WriteJSON(w, r, http.StatusOK, healthResponse{Status: "ok"})
}
@@ -0,0 +1,18 @@
package middleware
import (
"context"
"net/http"
"time"
)
// Timeout ограничивает время выполнения запроса.
func Timeout(d time.Duration) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), d)
defer cancel()
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
+8 -11
View File
@@ -2,6 +2,10 @@ package http
import (
"net/http"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"github.com/charawein/mtusi2026lab2/internal/service"
"github.com/charawein/mtusi2026lab2/internal/transport/http/middleware"
@@ -12,12 +16,14 @@ func NewRouter(
chats *service.ChatService,
messages *service.MessageService,
auth *service.AuthService,
pool *pgxpool.Pool,
redisClient *redis.Client,
) http.Handler {
userH := NewUserHandler(users)
chatH := NewChatHandler(chats)
msgH := NewMessageHandler(messages)
authH := NewAuthHandler(auth)
healthH := NewHealthHandler()
healthH := NewHealthHandler(pool, redisClient)
mux := http.NewServeMux()
@@ -26,16 +32,6 @@ func NewRouter(
mux.HandleFunc("POST /api/v1/users", userH.CreateUser)
mux.HandleFunc("POST /api/v1/auth/sessions", authH.CreateSession)
// Debug endpoints для демонстрации трека 5.1 (не входят в публичный контракт).
mux.HandleFunc("POST /debug/cache/on", func(w http.ResponseWriter, r *http.Request) {
messages.SetInvalidate(true)
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("POST /debug/cache/off", func(w http.ResponseWriter, r *http.Request) {
messages.SetInvalidate(false)
w.WriteHeader(http.StatusNoContent)
})
authMW := middleware.Auth(auth)
mux.Handle("DELETE /api/v1/auth/sessions/current", authMW(http.HandlerFunc(authH.RevokeSession)))
@@ -48,6 +44,7 @@ func NewRouter(
handler := middleware.Logging(mux)
handler = middleware.Recovery(handler)
handler = middleware.Timeout(5 * time.Second)(handler)
handler = middleware.RequestID(handler)
return handler
}