forked from mtusi2026/mtusi2026lab3
65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package app
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"mtusi2026-messenger/internal/handler"
|
|
"mtusi2026-messenger/internal/middleware"
|
|
"mtusi2026-messenger/internal/service"
|
|
)
|
|
|
|
func NewRouter(
|
|
log *slog.Logger,
|
|
instanceID string,
|
|
maxInflightRequests int,
|
|
db handler.Pinger,
|
|
authService *service.AuthService,
|
|
userService *service.UserService,
|
|
chatService *service.ChatService,
|
|
messageService *service.MessageService,
|
|
) http.Handler {
|
|
router := chi.NewRouter()
|
|
|
|
healthHandler := handler.NewHealthHandler(log, db)
|
|
authHandler := handler.NewAuthHandler(authService)
|
|
userHandler := handler.NewUserHandler(userService)
|
|
chatHandler := handler.NewChatHandler(chatService)
|
|
messageHandler := handler.NewMessageHandler(messageService)
|
|
|
|
router.Use(middleware.InstanceID(instanceID))
|
|
router.Use(middleware.RequestTimeout(5 * time.Second))
|
|
router.Use(middleware.RequestID)
|
|
router.Use(middleware.Logging(log))
|
|
router.Use(middleware.ConcurrencyLimit(maxInflightRequests))
|
|
|
|
router.Get("/health/live", healthHandler.Live)
|
|
router.Get("/health/ready", healthHandler.Ready)
|
|
|
|
router.Post("/api/v1/users", userHandler.Create)
|
|
|
|
router.Post("/api/v1/auth/sessions", authHandler.CreateSession)
|
|
router.Delete("/api/v1/auth/sessions/current", authHandler.RevokeSession)
|
|
|
|
router.Group(func(r chi.Router) {
|
|
r.Use(middleware.Auth(authService))
|
|
|
|
r.Get("/api/v1/users/me", userHandler.Me)
|
|
|
|
r.Post("/api/v1/chats", chatHandler.Create)
|
|
r.Get("/api/v1/chats", chatHandler.List)
|
|
r.Get("/api/v1/chats/{chat_id}", chatHandler.Get)
|
|
|
|
r.Post("/api/v1/chats/{chat_id}/messages", messageHandler.Create)
|
|
r.Get("/api/v1/chats/{chat_id}/messages", messageHandler.List)
|
|
})
|
|
|
|
router.Handle("/swagger/*", http.StripPrefix("/swagger/", http.FileServer(http.Dir("web/swagger"))))
|
|
router.Handle("/contracts/*", http.StripPrefix("/contracts/", http.FileServer(http.Dir("contracts"))))
|
|
|
|
return router
|
|
}
|