forked from mtusi2026/mtusi2026lab3
89 lines
3.1 KiB
Go
Executable File
89 lines
3.1 KiB
Go
Executable File
// Package internal wires HTTP routes, middleware and storage into one Fiber
|
|
// application and owns the process-wide error handler.
|
|
package internal
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"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/session"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/users"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/gofiber/fiber/v3/middleware/recover"
|
|
"github.com/gofiber/fiber/v3/middleware/requestid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.App {
|
|
st := store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL)
|
|
|
|
app := fiber.New(fiber.Config{
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
BodyLimit: 1 * 1024 * 1024,
|
|
ErrorHandler: handleError,
|
|
})
|
|
|
|
app.Use(recover.New())
|
|
app.Use(requestid.New())
|
|
app.Use(func(c fiber.Ctx) error {
|
|
c.Set("X-Instance-Id", cfg.InstanceID)
|
|
return c.Next()
|
|
})
|
|
|
|
healthHandler := health.NewHandler(st, sessions)
|
|
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)
|
|
|
|
api := app.Group("/api/v1")
|
|
api.Post("/users", usersHandler.CreateUser)
|
|
api.Post("/auth/sessions", authHandler.CreateSession)
|
|
|
|
protected := api.Group("", auth.BearerAuth(sessions))
|
|
protected.Get("/users/me", usersHandler.GetCurrentUser)
|
|
protected.Delete("/auth/sessions/current", authHandler.RevokeSession)
|
|
protected.Get("/chats", chatsHandler.ListChats)
|
|
protected.Post("/chats", chatsHandler.CreateChat)
|
|
protected.Get("/chats/:chat_id", chatsHandler.GetChat)
|
|
protected.Get("/chats/:chat_id/messages", messagesHandler.ListMessages)
|
|
protected.Post("/chats/:chat_id/messages", messagesHandler.CreateMessage)
|
|
|
|
return app
|
|
}
|
|
|
|
// handleError is the only writer of error responses: handlers and the store
|
|
// return *common.AppError and this function decides status, headers and body.
|
|
func handleError(c fiber.Ctx, err error) error {
|
|
if appErr, ok := errors.AsType[*common.AppError](err); ok {
|
|
if appErr.Challenge != "" {
|
|
c.Set(fiber.HeaderWWWAuthenticate, appErr.Challenge)
|
|
}
|
|
return common.SendError(c, appErr.Status, appErr.Code, appErr.Message)
|
|
}
|
|
|
|
if fiberErr, ok := errors.AsType[*fiber.Error](err); ok {
|
|
return common.SendError(c, fiberErr.Code,
|
|
common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message)
|
|
}
|
|
|
|
slog.Error("unhandled request error",
|
|
"request_id", requestid.FromContext(c), "err", err)
|
|
return common.SendError(c, fiber.StatusInternalServerError,
|
|
common.ErrorCodeInternal, "Internal server error")
|
|
}
|