forked from mtusi2026/mtusi2026lab3
feat(app) lab 3 implemented
This commit is contained in:
+18
-16
@@ -1,18 +1,20 @@
|
||||
// Package internal wires HTTP routes, middleware and storage into one Fiber
|
||||
// application and owns the process-wide error handler.
|
||||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/auth"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/chats"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/config"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/health"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/messages"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/users"
|
||||
"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"
|
||||
@@ -24,7 +26,7 @@ func SignalGracefulShutdown() {
|
||||
}
|
||||
|
||||
func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.App {
|
||||
st := store.New(pool, cfg.DBTimeout)
|
||||
st := store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL)
|
||||
|
||||
app := fiber.New(fiber.Config{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
@@ -36,6 +38,10 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
|
||||
|
||||
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()
|
||||
})
|
||||
|
||||
health.RegisterRoutes(app.Group("/health"), st, sessions)
|
||||
|
||||
@@ -53,16 +59,12 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
|
||||
return app
|
||||
}
|
||||
|
||||
// handleError keeps every failure in the contract error envelope, including
|
||||
// Fiber's own errors (404, 405, 413) and recovered panics.
|
||||
func handleError(c fiber.Ctx, err error) error {
|
||||
var appErr *common.AppError
|
||||
if errors.As(err, &appErr) {
|
||||
if appErr, ok := errors.AsType[*common.AppError](err); ok {
|
||||
return common.SendError(c, appErr.Status, appErr.Code, appErr.Message)
|
||||
}
|
||||
|
||||
var fiberErr *fiber.Error
|
||||
if errors.As(err, &fiberErr) {
|
||||
if fiberErr, ok := errors.AsType[*fiber.Error](err); ok {
|
||||
return common.SendError(c, fiberErr.Code,
|
||||
common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user