forked from mtusi2026/mtusi2026lab2
feat(app) lab2 prepared for mr
This commit is contained in:
+29
-43
@@ -2,59 +2,43 @@ package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/chats"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/health"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/messages"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/storage"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/users"
|
||||
"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"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/recover"
|
||||
"github.com/gofiber/fiber/v3/middleware/requestid"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func SignalGracefulShutdown() {
|
||||
health.SignalShutdown()
|
||||
}
|
||||
|
||||
func authMiddleware(c fiber.Ctx) error {
|
||||
userIDStr := c.Get("X-User-Id")
|
||||
if userIDStr == "" {
|
||||
return common.SendError(c, fiber.StatusUnauthorized,
|
||||
common.ErrorCodeUnauthorized, "X-User-Id header is required")
|
||||
}
|
||||
func New(cfg config.Config, pool *pgxpool.Pool, sessions *session.Store) *fiber.App {
|
||||
st := store.New(pool, cfg.DBTimeout)
|
||||
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
return common.SendError(c, fiber.StatusBadRequest,
|
||||
common.ErrorCodeInvalidRequest, "X-User-Id must be a valid UUID")
|
||||
}
|
||||
|
||||
var exists bool
|
||||
|
||||
err = storage.GlobalStore.View(func(tx storage.ReadOnlyTx) error {
|
||||
_, exists = tx.GetUser(userID)
|
||||
return nil
|
||||
})
|
||||
if err != nil || !exists {
|
||||
return common.SendError(c, fiber.StatusUnauthorized,
|
||||
common.ErrorCodeUnauthorized, "user not found")
|
||||
}
|
||||
|
||||
c.Locals("userID", userID)
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func GetApp() *fiber.App {
|
||||
app := fiber.New(fiber.Config{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
BodyLimit: 1 * 1024 * 1024,
|
||||
ErrorHandler: func(c fiber.Ctx, err error) error {
|
||||
if appErr, ok := errors.AsType[*common.AppError](err); ok {
|
||||
var appErr *common.AppError
|
||||
if errors.As(err, &appErr) {
|
||||
return common.SendError(c, appErr.Status, appErr.Code, appErr.Message)
|
||||
}
|
||||
|
||||
if fiberErr, ok := errors.AsType[*fiber.Error](err); ok {
|
||||
var fiberErr *fiber.Error
|
||||
if errors.As(err, &fiberErr) {
|
||||
return common.SendError(c, fiberErr.Code,
|
||||
common.ErrorCodeFromStatus(fiberErr.Code), fiberErr.Message)
|
||||
}
|
||||
@@ -67,16 +51,18 @@ func GetApp() *fiber.App {
|
||||
app.Use(recover.New())
|
||||
app.Use(requestid.New())
|
||||
|
||||
health.RegisterRoutes(app.Group("/health"))
|
||||
health.RegisterRoutes(app.Group("/health"), st, sessions)
|
||||
|
||||
api := app.Group("/api/v1")
|
||||
users.RegisterPublicRoutes(api)
|
||||
users.RegisterPublicRoutes(api, st)
|
||||
auth.RegisterPublicRoutes(api, st, sessions)
|
||||
|
||||
protected := api.Group("")
|
||||
protected.Use(authMiddleware)
|
||||
users.RegisterRoutes(protected)
|
||||
chats.RegisterRoutes(protected)
|
||||
messages.RegisterRoutes(protected)
|
||||
protected.Use(auth.BearerAuth(sessions))
|
||||
auth.RegisterRoutes(protected, st, sessions)
|
||||
users.RegisterRoutes(protected, st)
|
||||
chats.RegisterRoutes(protected, st)
|
||||
messages.RegisterRoutes(protected, st)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user