forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
+27
-13
@@ -4,6 +4,7 @@ package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/auth"
|
||||
@@ -21,10 +22,6 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func SignalGracefulShutdown() {
|
||||
health.SignalShutdown()
|
||||
}
|
||||
|
||||
func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.App {
|
||||
st := store.New(pool, cfg.DBTimeout, cfg.IdempotencyTTL)
|
||||
|
||||
@@ -43,24 +40,39 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
|
||||
return c.Next()
|
||||
})
|
||||
|
||||
health.RegisterRoutes(app.Group("/health"), st, sessions)
|
||||
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")
|
||||
users.RegisterPublicRoutes(api, st)
|
||||
auth.RegisterPublicRoutes(api, st, sessions)
|
||||
api.Post("/users", usersHandler.CreateUser)
|
||||
api.Post("/auth/sessions", authHandler.CreateSession)
|
||||
|
||||
protected := api.Group("")
|
||||
protected.Use(auth.BearerAuth(sessions))
|
||||
auth.RegisterProtectedRoutes(protected, st, sessions)
|
||||
users.RegisterProtectedRoutes(protected, st)
|
||||
chats.RegisterProtectedRoutes(protected, st)
|
||||
messages.RegisterProtectedRoutes(protected, st)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -69,6 +81,8 @@ func handleError(c fiber.Ctx, err error) error {
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user