forked from mtusi2026/mtusi2026lab5
ЛР 5: асинхронная обработка изображений, уровень 5
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http/dto"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http/middleware"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/service"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
||||
)
|
||||
|
||||
type usersHandler struct {
|
||||
users service.UsersService
|
||||
}
|
||||
|
||||
func (h *usersHandler) create(c echo.Context) error {
|
||||
var req dto.CreateUserRequest
|
||||
if err := decodeJSON(c.Request().Body, &req); err != nil {
|
||||
return WriteError(c, err)
|
||||
}
|
||||
|
||||
user, err := h.users.Register(c.Request().Context(), req.Username, req.DisplayName, req.Password)
|
||||
if err != nil {
|
||||
return WriteError(c, err)
|
||||
}
|
||||
return c.JSON(http.StatusCreated, toUserResponse(user))
|
||||
}
|
||||
|
||||
func (h *usersHandler) me(c echo.Context) error {
|
||||
callerID := c.Get(middleware.AuthUserIDKey).(string)
|
||||
user, err := h.users.Me(c.Request().Context(), callerID)
|
||||
if err != nil {
|
||||
return WriteError(c, err)
|
||||
}
|
||||
return c.JSON(http.StatusOK, toUserResponse(user))
|
||||
}
|
||||
|
||||
func toUserResponse(u domain.User) dto.UserResponse {
|
||||
return dto.UserResponse{
|
||||
ID: u.ID,
|
||||
Username: u.Username,
|
||||
DisplayName: u.DisplayName,
|
||||
CreatedAt: dto.NewTimestamp(u.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func decodeJSON(body io.Reader, dst any) error {
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrPayloadTooLarge) {
|
||||
return err
|
||||
}
|
||||
return &validate.Error{Field: "body", Reason: "cannot read body"}
|
||||
}
|
||||
if !utf8.Valid(data) {
|
||||
return &validate.Error{Field: "body", Reason: "invalid UTF-8"}
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(data))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(dst); err != nil {
|
||||
return &validate.Error{Field: "body", Reason: jsonErrorReason(err)}
|
||||
}
|
||||
if _, err := dec.Token(); !errors.Is(err, io.EOF) {
|
||||
return &validate.Error{Field: "body", Reason: "unexpected data after JSON object"}
|
||||
}
|
||||
return checkExactKeys(data, dst)
|
||||
}
|
||||
|
||||
func checkExactKeys(data []byte, dst any) error {
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &obj); err != nil {
|
||||
return nil
|
||||
}
|
||||
allowed := make(map[string]struct{})
|
||||
t := reflect.TypeOf(dst).Elem()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
name, _, _ := strings.Cut(t.Field(i).Tag.Get("json"), ",")
|
||||
allowed[name] = struct{}{}
|
||||
}
|
||||
for key := range obj {
|
||||
if _, ok := allowed[key]; !ok {
|
||||
return &validate.Error{Field: "body", Reason: fmt.Sprintf("unknown field %q", key)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func jsonErrorReason(err error) string {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return "empty body"
|
||||
}
|
||||
var typeErr *json.UnmarshalTypeError
|
||||
if errors.As(err, &typeErr) {
|
||||
if typeErr.Field == "" {
|
||||
return "expected a JSON object"
|
||||
}
|
||||
return fmt.Sprintf("field %q: invalid type", typeErr.Field)
|
||||
}
|
||||
if name, ok := strings.CutPrefix(err.Error(), "json: unknown field "); ok {
|
||||
return "unknown field " + name
|
||||
}
|
||||
return "invalid JSON"
|
||||
}
|
||||
Reference in New Issue
Block a user