forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package fiber
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
||||
)
|
||||
|
||||
// Attachments defines file operations required by the HTTP boundary.
|
||||
type Attachments interface {
|
||||
// Upload validates and publishes an original.
|
||||
Upload(context.Context, string, string, string, string, []byte) (dto.Attachment, error)
|
||||
// Get authorizes access to attachment metadata.
|
||||
Get(context.Context, string, string) (dto.Attachment, error)
|
||||
// Download returns an authorized original stream.
|
||||
Download(context.Context, string, string, string) (dto.Attachment, io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// WithAttachments binds the file service during composition before server startup.
|
||||
func (ctrl *Controller) WithAttachments(attachments Attachments) *Controller {
|
||||
ctrl.attachments = attachments
|
||||
return ctrl
|
||||
}
|
||||
|
||||
type attachmentResponse struct {
|
||||
// ID identifies the uploaded file.
|
||||
ID string `json:"id"`
|
||||
// ChatID defines the chat authorization scope.
|
||||
ChatID string `json:"chat_id"`
|
||||
// OwnerID identifies the original uploader.
|
||||
OwnerID string `json:"owner_id"`
|
||||
// Filename is the validated display name.
|
||||
Filename string `json:"filename"`
|
||||
// ContentType describes the verified original format.
|
||||
ContentType string `json:"content_type"`
|
||||
// SizeBytes is the encoded original length.
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
// SHA256 is the original lowercase checksum.
|
||||
SHA256 string `json:"sha256"`
|
||||
// Status is the durable publication state.
|
||||
Status string `json:"status"`
|
||||
// Variants lists generated variants, empty in lab4.
|
||||
Variants []dto.Variant `json:"variants"`
|
||||
// ErrorCode is null for a ready original.
|
||||
ErrorCode *string `json:"error_code"`
|
||||
// CreatedAt is the reservation time.
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// UpdatedAt is the publication time.
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// attachmentResponseOf deliberately omits private S3 coordinates.
|
||||
func attachmentResponseOf(a dto.Attachment) attachmentResponse {
|
||||
return attachmentResponse{
|
||||
ID: a.ID.String(),
|
||||
ChatID: a.ChatID.String(),
|
||||
OwnerID: a.OwnerID.String(),
|
||||
Filename: a.Filename,
|
||||
ContentType: a.ContentType,
|
||||
SizeBytes: a.SizeBytes,
|
||||
SHA256: a.SHA256,
|
||||
Status: a.Status,
|
||||
Variants: publicVariants(a.Variants),
|
||||
ErrorCode: a.ErrorCode,
|
||||
CreatedAt: a.CreatedAt,
|
||||
UpdatedAt: a.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// handleUpload passes raw bounded bytes to application validation.
|
||||
func (ctrl *Controller) handleUpload(ctx *fiber.Ctx) error {
|
||||
if ctrl.attachments == nil {
|
||||
return service.ErrUnavailable
|
||||
}
|
||||
a, err := ctrl.attachments.Upload(ctx.UserContext(), identity(ctx), ctx.Params("chat_id"), ctx.Query("filename"), ctx.Get("Content-Type"), ctx.Body())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.Status(http.StatusAccepted).JSON(attachmentResponseOf(a))
|
||||
}
|
||||
|
||||
// handleAttachment returns only the public metadata of an accessible file.
|
||||
func (ctrl *Controller) handleAttachment(ctx *fiber.Ctx) error {
|
||||
if ctrl.attachments == nil {
|
||||
return service.ErrUnavailable
|
||||
}
|
||||
a, err := ctrl.attachments.Get(ctx.UserContext(), identity(ctx), ctx.Params("id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.JSON(attachmentResponseOf(a))
|
||||
}
|
||||
|
||||
// handleContent streams original bytes without redirecting around authorization.
|
||||
func (ctrl *Controller) handleContent(ctx *fiber.Ctx) error {
|
||||
if ctrl.attachments == nil {
|
||||
return service.ErrUnavailable
|
||||
}
|
||||
a, reader, err := ctrl.attachments.Download(ctx.UserContext(), identity(ctx), ctx.Params("id"), ctx.Query("variant"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.Set("Content-Type", a.ContentType)
|
||||
ctx.Set("X-Content-Type-Options", "nosniff")
|
||||
return ctx.SendStream(&responseStream{Reader: reader, cleanup: func() {
|
||||
}}, int(a.SizeBytes))
|
||||
}
|
||||
|
||||
// publicVariants copies generated metadata without exposing storage keys.
|
||||
func publicVariants(variants []dto.Variant) []dto.Variant {
|
||||
result := make([]dto.Variant, 0, len(variants))
|
||||
for _, variant := range variants {
|
||||
variant.ObjectKey = ""
|
||||
result = append(result, variant)
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user