forked from mtusi2026/mtusi2026lab6
67 lines
2.9 KiB
Go
67 lines
2.9 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"github.com/charawein/mtusi2026lab6/internal/service"
|
|
"github.com/charawein/mtusi2026lab6/internal/transport/http/middleware"
|
|
)
|
|
|
|
func NewRouter(
|
|
users *service.UserService,
|
|
chats *service.ChatService,
|
|
messages *service.MessageService,
|
|
attachments *service.AttachmentService,
|
|
auth *service.AuthService,
|
|
pool *pgxpool.Pool,
|
|
redisClient *redis.Client,
|
|
instanceID string,
|
|
draining *atomic.Bool,
|
|
) http.Handler {
|
|
userH := NewUserHandler(users)
|
|
chatH := NewChatHandler(chats)
|
|
msgH := NewMessageHandler(messages)
|
|
authH := NewAuthHandler(auth)
|
|
attH := NewAttachmentHandler(attachments)
|
|
healthH := NewHealthHandler(pool, redisClient, draining)
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// Таймаут на запрос: обычный API — 5 s, передача файлов — 30 s.
|
|
short := middleware.Timeout(5 * time.Second)
|
|
long := middleware.Timeout(30 * time.Second)
|
|
authMW := func(h http.Handler) http.Handler { return short(middleware.Auth(auth)(h)) }
|
|
fileMW := func(h http.Handler) http.Handler { return long(middleware.Auth(auth)(h)) }
|
|
|
|
mux.Handle("GET /health/live", short(http.HandlerFunc(healthH.Live)))
|
|
mux.Handle("GET /health/ready", short(http.HandlerFunc(healthH.Ready)))
|
|
mux.Handle("POST /api/v1/users", short(http.HandlerFunc(userH.CreateUser)))
|
|
mux.Handle("POST /api/v1/auth/sessions", short(http.HandlerFunc(authH.CreateSession)))
|
|
|
|
mux.Handle("DELETE /api/v1/auth/sessions/current", authMW(http.HandlerFunc(authH.RevokeSession)))
|
|
mux.Handle("GET /api/v1/users/me", authMW(http.HandlerFunc(userH.CurrentUser)))
|
|
mux.Handle("POST /api/v1/chats", authMW(http.HandlerFunc(chatH.CreateChat)))
|
|
mux.Handle("GET /api/v1/chats", authMW(http.HandlerFunc(chatH.ListChats)))
|
|
mux.Handle("GET /api/v1/chats/{chat_id}", authMW(http.HandlerFunc(chatH.GetChat)))
|
|
mux.Handle("POST /api/v1/chats/{chat_id}/messages", authMW(http.HandlerFunc(msgH.SendMessage)))
|
|
mux.Handle("GET /api/v1/chats/{chat_id}/messages", authMW(http.HandlerFunc(msgH.ListMessages)))
|
|
mux.Handle("POST /api/v1/chats/{chat_id}/attachments", fileMW(http.HandlerFunc(attH.Upload)))
|
|
mux.Handle("GET /api/v1/attachments/{attachment_id}", authMW(http.HandlerFunc(attH.Get)))
|
|
mux.Handle("GET /api/v1/attachments/{attachment_id}/content", fileMW(http.HandlerFunc(attH.Content)))
|
|
|
|
// Telemetry → Recovery → Logging → mux: все middleware до mux передают тот же
|
|
// *Request, поэтому шаблон маршрута (r.Pattern) виден после обработки;
|
|
// паника, превращённая Recovery в 500, учитывается в span и метриках.
|
|
handler := middleware.Logging(mux)
|
|
handler = middleware.Recovery(handler)
|
|
handler = middleware.Telemetry(handler)
|
|
handler = middleware.RequestID(handler)
|
|
handler = middleware.InstanceID(instanceID)(handler)
|
|
return handler
|
|
}
|