(ctx context.Context, l cloudsql.Logger, addr string, mux *http.ServeMux, shutdownCh chan<- error)
| 1276 | } |
| 1277 | |
| 1278 | func startHTTPServer(ctx context.Context, l cloudsql.Logger, addr string, mux *http.ServeMux, shutdownCh chan<- error) { |
| 1279 | server := &http.Server{ |
| 1280 | Addr: addr, |
| 1281 | Handler: mux, |
| 1282 | } |
| 1283 | // Start the HTTP server. |
| 1284 | go func() { |
| 1285 | err := server.ListenAndServe() |
| 1286 | if errors.Is(err, http.ErrServerClosed) { |
| 1287 | return |
| 1288 | } |
| 1289 | if err != nil { |
| 1290 | shutdownCh <- fmt.Errorf("failed to start HTTP server: %v", err) |
| 1291 | } |
| 1292 | }() |
| 1293 | // Handle shutdown of the HTTP server gracefully. |
| 1294 | <-ctx.Done() |
| 1295 | // Give the HTTP server a second to shut down cleanly. |
| 1296 | ctx2, cancel := context.WithTimeout(context.Background(), time.Second) |
| 1297 | defer cancel() |
| 1298 | if err := server.Shutdown(ctx2); err != nil { |
| 1299 | l.Errorf("failed to shutdown HTTP server: %v\n", err) |
| 1300 | } |
| 1301 | } |
no test coverage detected