Отвязать жизненный цикл сервера от сборки приложения

This commit is contained in:
2026-10-06 14:50:12 +03:00
parent 6720108793
commit 46c00436a0
3 changed files with 28 additions and 12 deletions
+8 -5
View File
@@ -10,14 +10,17 @@ import (
"net"
"net/http"
"time"
"messenger/internal/app"
)
// Readiness is switched off when shutdown starts.
type Readiness interface {
SetReady(ready bool)
}
// Run serves until ctx is cancelled, then shuts down within shutdownTimeout.
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shutdownTimeout time.Duration) error {
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler, ready Readiness, shutdownTimeout time.Duration) error {
srv := &http.Server{
Handler: a.Engine,
Handler: h,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
@@ -43,7 +46,7 @@ func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shu
}
log.Info("shutdown started", "timeout", shutdownTimeout.String())
a.Health.SetReady(false)
ready.SetReady(false)
drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(drain); err != nil {