ЛР 2 — БПИ2301 — Каменски — на 5 #20

Closed
kmwk10 wants to merge 40 commits from kmwk10/mtusi2026lab2:solution/BPI2301-kamenski into master
2 changed files with 19 additions and 0 deletions
Showing only changes of commit 2d413ab704 - Show all commits
+2
View File
@@ -3,6 +3,7 @@ package app
import (
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5"
@@ -28,6 +29,7 @@ func NewRouter(
chatHandler := handler.NewChatHandler(chatService)
messageHandler := handler.NewMessageHandler(messageService)
router.Use(middleware.RequestTimeout(5 * time.Second))
router.Use(middleware.RequestID)
router.Use(middleware.Logging(log))
router.Use(middleware.ConcurrencyLimit(maxInflightRequests))
+17
View File
@@ -0,0 +1,17 @@
package middleware
import (
"context"
"net/http"
"time"
)
func RequestTimeout(d time.Duration) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), d)
defer cancel()
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}