feat: add data validation and structured logging

This commit is contained in:
2026-01-05 18:24:11 +03:00
parent 9bf2a15045
commit 9e2657c709
7 changed files with 122 additions and 27 deletions

View File

@@ -8,6 +8,7 @@ import (
"gitea.mrixs.me/minecraft-platform/backend/internal/core"
"gitea.mrixs.me/minecraft-platform/backend/internal/models"
"gitea.mrixs.me/minecraft-platform/backend/internal/utils"
)
type AuthHandler struct {
@@ -27,6 +28,13 @@ func (h *AuthHandler) Authenticate(w http.ResponseWriter, r *http.Request) {
return
}
if validationErrors := utils.ValidateStruct(req); validationErrors != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(validationErrors)
return
}
response, err := h.Service.Authenticate(r.Context(), req)
if err != nil {
if errors.Is(err, core.ErrInvalidCredentials) {
@@ -57,6 +65,13 @@ func (h *AuthHandler) Join(w http.ResponseWriter, r *http.Request) {
return
}
if validationErrors := utils.ValidateStruct(req); validationErrors != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(validationErrors)
return
}
err := h.Service.ValidateJoinRequest(r.Context(), req)
if err != nil {
if errors.Is(err, core.ErrInvalidCredentials) {
@@ -84,6 +99,13 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
return
}
if validationErrors := utils.ValidateStruct(req); validationErrors != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(validationErrors)
return
}
token, user, err := h.Service.LoginUser(r.Context(), req)
if err != nil {
if errors.Is(err, core.ErrInvalidCredentials) {

View File

@@ -4,11 +4,13 @@ import (
"encoding/json"
"errors"
"log"
"log/slog"
"net/http"
"gitea.mrixs.me/minecraft-platform/backend/internal/core"
"gitea.mrixs.me/minecraft-platform/backend/internal/database"
"gitea.mrixs.me/minecraft-platform/backend/internal/models"
"gitea.mrixs.me/minecraft-platform/backend/internal/utils"
"github.com/golang-jwt/jwt/v5"
)
@@ -23,13 +25,18 @@ func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
return
}
// --- ДОБАВЛЕНО ЛОГИРОВАНИЕ ---
log.Printf("[Handler] Received registration request for username: '%s', email: '%s'", req.Username, req.Email)
if validationErrors := utils.ValidateStruct(req); validationErrors != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(validationErrors)
return
}
slog.Info("Received registration request", "username", req.Username, "email", req.Email)
err := h.Service.RegisterNewUser(r.Context(), req)
if err != nil {
// --- ДОБАВЛЕНО ЛОГИРОВАНИЕ ОШИБКИ ---
log.Printf("[Handler] Service returned error: %v", err)
slog.Error("Service returned error", "error", err)
switch {
case errors.Is(err, database.ErrUserExists):
@@ -42,8 +49,7 @@ func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
return
}
// --- ДОБАВЛЕНО ЛОГИРОВАНИЕ УСПЕХА ---
log.Printf("[Handler] User '%s' registered successfully.", req.Username)
slog.Info("User registered successfully", "username", req.Username)
w.WriteHeader(http.StatusCreated)
}