pomerium/internal/tests/xdserr/echo.go
backport-actions-token[bot] bdb8ebd470
skip configuration updates to the most recent one (#2690) (#2692)
Co-authored-by: Denis Mishin <dmishin@pomerium.com>
2021-10-21 11:41:59 -04:00

34 lines
620 B
Go

package xdserr
import (
"context"
"fmt"
"net"
"net/http"
"golang.org/x/sync/errgroup"
)
func echo(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "pong")
}
// RunEcho runs a test echo http server
func RunEcho(ctx context.Context) (string, error) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
return "", err
}
mux := http.NewServeMux()
mux.HandleFunc("/", echo)
srv := http.Server{
Handler: mux,
}
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error { return srv.Serve(l) })
eg.Go(func() error {
<-ctx.Done()
return srv.Close()
})
return l.Addr().String(), nil
}