refactor(app) AI refactoring before moving to Lab3 done

This commit is contained in:
imadev
2026-10-26 19:39:45 +03:00
parent d14fed291e
commit 71174f844c
30 changed files with 895 additions and 560 deletions
+23 -19
View File
@@ -31,21 +31,7 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
BodyLimit: 1 * 1024 * 1024,
ErrorHandler: func(c fiber.Ctx, err error) error {
var appErr *common.AppError
if errors.As(err, &appErr) {
return common.SendError(c, appErr.Status, appErr.Code, appErr.Message)
}
var fiberErr *fiber.Error
if errors.As(err, &fiberErr) {
return common.SendError(c, fiberErr.Code,
common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message)
}
return common.SendError(c, fiber.StatusInternalServerError,
common.ErrorCodeInternal, "Internal server error")
},
ErrorHandler: handleError,
})
app.Use(recover.New())
@@ -59,10 +45,28 @@ func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.
protected := api.Group("")
protected.Use(auth.BearerAuth(sessions))
auth.RegisterRoutes(protected, st, sessions)
users.RegisterRoutes(protected, st)
chats.RegisterRoutes(protected, st)
messages.RegisterRoutes(protected, st)
auth.RegisterProtectedRoutes(protected, st, sessions)
users.RegisterProtectedRoutes(protected, st)
chats.RegisterProtectedRoutes(protected, st)
messages.RegisterProtectedRoutes(protected, st)
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) {
return common.SendError(c, appErr.Status, appErr.Code, appErr.Message)
}
var fiberErr *fiber.Error
if errors.As(err, &fiberErr) {
return common.SendError(c, fiberErr.Code,
common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message)
}
return common.SendError(c, fiber.StatusInternalServerError,
common.ErrorCodeInternal, "Internal server error")
}