@@ -22,12 +22,12 @@ import (
2222// Serve starts the HTTP server.
2323//
2424// Deprecated: use the Caddy server module or the standalone library instead.
25- func (h * Hub ) Serve () { //nolint:funlen
25+ func (h * Hub ) Serve (ctx context. Context ) { //nolint:funlen
2626 addr := h .config .GetString ("addr" )
2727
2828 h .server = & http.Server {
2929 Addr : addr ,
30- Handler : h .baseHandler (),
30+ Handler : h .baseHandler (ctx ),
3131 ReadTimeout : h .config .GetDuration ("read_timeout" ),
3232 ReadHeaderTimeout : h .config .GetDuration ("read_header_timeout" ),
3333 WriteTimeout : h .config .GetDuration ("write_timeout" ),
@@ -44,22 +44,30 @@ func (h *Hub) Serve() { //nolint:funlen
4444 WriteTimeout : h .config .GetDuration ("write_timeout" ),
4545 }
4646
47- h .logger .Info ("Mercure metrics started" , slog .String ("addr" , addr ))
47+ if h .logger .Enabled (ctx , slog .LevelInfo ) {
48+ h .logger .LogAttrs (ctx , slog .LevelInfo , "Mercure metrics started" , slog .String ("addr" , addr ))
49+ }
4850
49- go h .metricsServer .ListenAndServe ()
51+ go func () {
52+ if err := h .metricsServer .ListenAndServe (); err != nil && ! errors .Is (err , http .ErrServerClosed ) && h .logger .Enabled (ctx , slog .LevelError ) { //nolint:gosec
53+ h .logger .LogAttrs (ctx , slog .LevelError , "Mercure metrics server error" , slog .Any ("error" , err ))
54+ }
55+ }()
5056 }
5157
5258 acme := len (h .allowedHosts ) > 0
5359
5460 certFile := h .config .GetString ("cert_file" )
5561 keyFile := h .config .GetString ("key_file" )
5662
57- done := h .listenShutdown ()
63+ done := h .listenShutdown (ctx )
5864
5965 var err error
6066
6167 if ! acme && certFile == "" && keyFile == "" { //nolint:nestif
62- h .logger .Info ("Mercure started" , slog .String ("protocol" , "http" ), slog .String ("addr" , addr ))
68+ if h .logger .Enabled (ctx , slog .LevelInfo ) {
69+ h .logger .LogAttrs (ctx , slog .LevelInfo , "Mercure started" , slog .String ("protocol" , "http" ), slog .String ("addr" , addr ))
70+ }
6371
6472 err = h .server .ListenAndServe ()
6573 } else {
@@ -78,23 +86,30 @@ func (h *Hub) Serve() { //nolint:funlen
7886 h .server .TLSConfig = certManager .TLSConfig ()
7987
8088 // Mandatory for Let's Encrypt http-01 challenge
81- go http .ListenAndServe (h .config .GetString ("acme_http01_addr" ), certManager .HTTPHandler (nil )) //nolint:gosec
89+ go func () {
90+ //nolint:gosec
91+ if err := http .ListenAndServe (h .config .GetString ("acme_http01_addr" ), certManager .HTTPHandler (nil )); err != nil && ! errors .Is (err , http .ErrServerClosed ) && h .logger .Enabled (ctx , slog .LevelError ) {
92+ h .logger .LogAttrs (ctx , slog .LevelError , "Error running HTTP endpoint" , slog .Any ("error" , err ))
93+ }
94+ }()
8295 }
8396
84- h .logger .Info ("Mercure started" , slog .String ("protocol" , "https" ), slog .String ("addr" , addr ))
97+ if h .logger .Enabled (ctx , slog .LevelInfo ) {
98+ h .logger .LogAttrs (ctx , slog .LevelInfo , "Mercure started" , slog .String ("protocol" , "https" ), slog .String ("addr" , addr ))
99+ }
85100
86101 err = h .server .ListenAndServeTLS (certFile , keyFile )
87102 }
88103
89- if ! errors .Is (err , http .ErrServerClosed ) {
90- h .logger .Error ( "Unexpected error" , slog .Any ("error" , err ))
104+ if ! errors .Is (err , http .ErrServerClosed ) && h . logger . Enabled ( ctx , slog . LevelError ) {
105+ h .logger .LogAttrs ( ctx , slog . LevelError , "Unexpected error" , slog .Any ("error" , err ))
91106 }
92107
93108 <- done
94109}
95110
96111// Deprecated: use the Caddy server module or the standalone library instead.
97- func (h * Hub ) listenShutdown () <- chan struct {} {
112+ func (h * Hub ) listenShutdown (ctx context. Context ) <- chan struct {} {
98113 idleConnsClosed := make (chan struct {})
99114
100115 h .server .RegisterOnShutdown (func () {
@@ -110,17 +125,19 @@ func (h *Hub) listenShutdown() <-chan struct{} {
110125 signal .Notify (sigint , os .Interrupt )
111126 <- sigint
112127
113- if err := h .server .Shutdown (context . Background ()) ; err != nil {
114- h .logger .Error ( "Unexpected error during server shutdown" , slog .Any ("error" , err ))
128+ if err := h .server .Shutdown (ctx ) ; err != nil && h . logger . Enabled ( ctx , slog . LevelError ) {
129+ h .logger .LogAttrs ( ctx , slog . LevelError , "Unexpected error during server shutdown" , slog .Any ("error" , err ))
115130 }
116131
117132 if h .metricsServer != nil {
118- if err := h .metricsServer .Shutdown (context . Background ()) ; err != nil {
119- h .logger .Error ( "Unexpected error during metrics server shutdown" , slog .Any ("error" , err ))
133+ if err := h .metricsServer .Shutdown (ctx ) ; err != nil && h . logger . Enabled ( ctx , slog . LevelError ) {
134+ h .logger .LogAttrs ( ctx , slog . LevelError , "Unexpected error during metrics server shutdown" , slog .Any ("error" , err ))
120135 }
121136 }
122137
123- h .logger .Info ("My Baby Shot Me Down" )
138+ if h .logger .Enabled (ctx , slog .LevelInfo ) {
139+ h .logger .LogAttrs (ctx , slog .LevelInfo , "My Baby Shot Me Down" )
140+ }
124141
125142 select {
126143 case <- idleConnsClosed :
@@ -135,9 +152,9 @@ func (h *Hub) listenShutdown() <-chan struct{} {
135152// chainHandlers configures and chains handlers.
136153//
137154// Deprecated: use the Caddy server module or the standalone library instead.
138- func (h * Hub ) chainHandlers () http.Handler { //nolint:funlen
155+ func (h * Hub ) chainHandlers (ctx context. Context ) http.Handler { //nolint:funlen
139156 r := mux .NewRouter ()
140- h .registerSubscriptionHandlers (context . Background () , r )
157+ h .registerSubscriptionHandlers (ctx , r )
141158
142159 r .HandleFunc (defaultHubURL , h .SubscribeHandler ).Methods (http .MethodGet , http .MethodHead )
143160 r .HandleFunc (defaultHubURL , h .PublishHandler ).Methods (http .MethodPost )
@@ -200,7 +217,7 @@ func (h *Hub) chainHandlers() http.Handler { //nolint:funlen
200217
201218 var loggingHandler http.Handler
202219
203- if h .logger .Enabled (context . Background () , slog .LevelError ) {
220+ if h .logger .Enabled (ctx , slog .LevelError ) {
204221 loggingHandler = handlers .CombinedLoggingHandler (os .Stderr , secureHandler )
205222 } else {
206223 loggingHandler = secureHandler
@@ -215,15 +232,15 @@ func (h *Hub) chainHandlers() http.Handler { //nolint:funlen
215232}
216233
217234// Deprecated: use the Caddy server module or the standalone library instead.
218- func (h * Hub ) baseHandler () http.Handler {
235+ func (h * Hub ) baseHandler (ctx context. Context ) http.Handler {
219236 mainRouter := mux .NewRouter ()
220237 mainRouter .UseEncodedPath ()
221238 mainRouter .SkipClean (true )
222239
223240 // Register /healthz (if enabled, in a way that doesn't pollute the HTTP logs).
224241 registerHealthz (mainRouter )
225242
226- handler := h .chainHandlers ()
243+ handler := h .chainHandlers (ctx )
227244 mainRouter .PathPrefix ("/" ).Handler (handler )
228245
229246 return mainRouter
0 commit comments