forked from mtusi2026/mtusi2026lab4
feat(app) implemented lab 4
This commit is contained in:
+44
-12
@@ -5,14 +5,15 @@ package internal
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/attachments"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/auth"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/chats"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/config"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/health"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/messages"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/session"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/users"
|
||||
@@ -22,15 +23,33 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.App {
|
||||
func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) (*fiber.App, error) {
|
||||
st := store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL)
|
||||
|
||||
objects, err := objectstore.New(objectstore.Config{
|
||||
Endpoint: cfg.S3Endpoint,
|
||||
Region: cfg.S3Region,
|
||||
Bucket: cfg.S3Bucket,
|
||||
AccessKey: cfg.S3AccessKey,
|
||||
SecretKey: cfg.S3SecretKey,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
app := fiber.New(fiber.Config{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
BodyLimit: 1 * 1024 * 1024,
|
||||
ErrorHandler: handleError,
|
||||
ReadTimeout: cfg.ReadTimeout,
|
||||
WriteTimeout: cfg.WriteTimeout,
|
||||
IdleTimeout: cfg.IdleTimeout,
|
||||
// BodyLimit is the coarse HTTP envelope. The attachment handler and
|
||||
// BindJSON enforce their own smaller budgets.
|
||||
BodyLimit: cfg.HTTPBodyLimit,
|
||||
// Without StreamRequestBody fasthttp buffers the entire body
|
||||
// before the handler runs. Streaming lets the upload path copy in
|
||||
// 32 KiB chunks and stops a 10 MiB body from becoming a per-request
|
||||
// allocation even when the request is later rejected.
|
||||
StreamRequestBody: true,
|
||||
ErrorHandler: handleError,
|
||||
})
|
||||
|
||||
app.Use(recover.New())
|
||||
@@ -40,15 +59,25 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
|
||||
return c.Next()
|
||||
})
|
||||
|
||||
healthHandler := health.NewHandler(st, sessions)
|
||||
healthHandler := health.NewHandler(st, sessions, objects)
|
||||
healthRoutes := app.Group("/health")
|
||||
healthRoutes.Get("/live", healthHandler.Live)
|
||||
healthRoutes.Get("/ready", healthHandler.Ready)
|
||||
|
||||
authHandler := auth.NewHandler(st, sessions)
|
||||
usersHandler := users.NewHandler(st)
|
||||
chatsHandler := chats.NewHandler(st)
|
||||
messagesHandler := messages.NewHandler(st)
|
||||
usersHandler := users.NewHandler(st, config.MaxJSONBodyBytes)
|
||||
chatsHandler := chats.NewHandler(st, config.MaxJSONBodyBytes)
|
||||
messagesHandler := messages.NewHandler(st, config.MaxJSONBodyBytes)
|
||||
attachmentsHandler := attachments.NewHandler(
|
||||
st,
|
||||
objects,
|
||||
attachments.NewValidator(cfg.MaxImagePixels, cfg.ImageDecodeBytes),
|
||||
cfg.MaxUploadBytes,
|
||||
cfg.UploadDrainLimit,
|
||||
cfg.UploadTimeout,
|
||||
cfg.DownloadTimeout,
|
||||
cfg.UploadTempDir,
|
||||
)
|
||||
|
||||
api := app.Group("/api/v1")
|
||||
api.Post("/users", usersHandler.CreateUser)
|
||||
@@ -62,8 +91,11 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
|
||||
protected.Get("/chats/:chat_id", chatsHandler.GetChat)
|
||||
protected.Get("/chats/:chat_id/messages", messagesHandler.ListMessages)
|
||||
protected.Post("/chats/:chat_id/messages", messagesHandler.CreateMessage)
|
||||
protected.Post("/chats/:chat_id/attachments", attachmentsHandler.Upload)
|
||||
protected.Get("/attachments/:attachment_id", attachmentsHandler.Get)
|
||||
protected.Get("/attachments/:attachment_id/content", attachmentsHandler.Download)
|
||||
|
||||
return app
|
||||
return app, nil
|
||||
}
|
||||
|
||||
// handleError is the only writer of error responses: handlers and the store
|
||||
|
||||
Reference in New Issue
Block a user