Отвязать жизненный цикл сервера от сборки приложения
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user