chore: port lab 2 solution

This commit is contained in:
kk10
2026-10-16 19:50:49 +03:00
parent 0ab3a341aa
commit 6e0829e628
71 changed files with 4305 additions and 6 deletions
+37
View File
@@ -0,0 +1,37 @@
package httpx
import (
"net/http"
"strings"
"mtusi2026-messenger/internal/dto"
)
const (
BasicChallenge = `Basic realm="messenger", charset="UTF-8"`
BearerChallenge = `Bearer realm="messenger"`
)
func BearerToken(r *http.Request) (string, bool) {
const prefix = "Bearer "
header := r.Header.Get("Authorization")
if len(header) <= len(prefix) || !strings.EqualFold(header[:len(prefix)], prefix) {
return "", false
}
token := strings.TrimSpace(header[len(prefix):])
return token, token != ""
}
func WriteUnauthorized(w http.ResponseWriter, challenge, requestID string) {
w.Header().Set("WWW-Authenticate", challenge)
WriteError(
w,
http.StatusUnauthorized,
dto.ErrorCodeUnauthorized,
"unauthorized",
requestID,
)
}
+34
View File
@@ -0,0 +1,34 @@
package httpx
import (
"net/http"
"strconv"
"mtusi2026-messenger/internal/dto"
)
func ParsePagination(w http.ResponseWriter, r *http.Request, requestID string) (limit int, cursor *string, ok bool) {
q := r.URL.Query()
limit = 50
if raw := q.Get("limit"); raw != "" {
v, err := strconv.Atoi(raw)
if err != nil || v < 1 || v > 100 {
WriteError(
w,
http.StatusBadRequest,
dto.ErrorCodeInvalidRequest,
"limit must be an integer between 1 and 100",
requestID,
)
return 0, nil, false
}
limit = v
}
if raw := q.Get("cursor"); raw != "" {
cursor = &raw
}
return limit, cursor, true
}
+36
View File
@@ -0,0 +1,36 @@
package httpx
import (
"encoding/json"
"errors"
"io"
"mime"
"net/http"
"mtusi2026-messenger/internal/dto"
)
func DecodeJSONBody(w http.ResponseWriter, r *http.Request, requestID string, dst any) bool {
if ct := r.Header.Get("Content-Type"); ct != "" {
mt, _, err := mime.ParseMediaType(ct)
if err != nil || mt != "application/json" {
WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "expected application/json", requestID)
return false
}
}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(dst); err != nil {
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
return false
}
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
return false
}
return true
}
+67
View File
@@ -0,0 +1,67 @@
package httpx
import (
"context"
"encoding/json"
"errors"
"net"
"net/http"
"strings"
"github.com/jackc/pgx/v5/pgconn"
"mtusi2026-messenger/internal/dto"
)
func WriteJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}
func WriteError(w http.ResponseWriter, status int, code dto.ErrorCode, message, requestID string) {
WriteJSON(w, status, dto.ErrorResponse{
Error: dto.Error{
Code: code,
Message: message,
RequestID: requestID,
},
})
}
func WriteInternalError(w http.ResponseWriter, requestID string, err error) {
if isUnavailable(err) {
WriteError(
w,
http.StatusServiceUnavailable,
dto.ErrorCodeUnavailable,
"dependency unavailable",
requestID,
)
return
}
WriteError(
w,
http.StatusInternalServerError,
dto.ErrorCodeInternalError,
"internal server error",
requestID,
)
}
func isUnavailable(err error) bool {
if errors.Is(err, context.DeadlineExceeded) {
return true
}
if _, ok := errors.AsType[net.Error](err); ok {
return true
}
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
return strings.HasPrefix(pgErr.Code, "08") || pgErr.Code == "57P01"
}
return false
}