(ctx context.Context, port int)
| 10 | ) |
| 11 | |
| 12 | func ServeHTTP(ctx context.Context, port int) error { |
| 13 | // A server without a handler will serve http.DefaultServeMux |
| 14 | addr := ":" + fmt.Sprint(port) |
| 15 | srv := &http.Server{Addr: addr} |
| 16 | |
| 17 | // Run server in a goroutine |
| 18 | errCh := make(chan error, 1) |
| 19 | go func() { |
| 20 | errCh <- srv.ListenAndServe() |
| 21 | }() |
| 22 | |
| 23 | // Handle errors and context cancellation |
| 24 | select { |
| 25 | case err := <-errCh: |
| 26 | return err |
| 27 | case <-ctx.Done(): |
| 28 | _ = srv.Close() |
| 29 | return ctx.Err() |
| 30 | } |
| 31 | } |