From 5f89c932749e28093629c6cfedf58a9aa532ee89 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Tue, 17 Sep 2024 12:59:28 -0700 Subject: [PATCH] safeweb: add a ListenAndServe method to the Server type (#13498) Updates #13497 Change-Id: I398e9fa58ad0b9dc799ea280c9c7a32150150ee4 Signed-off-by: M. J. Fromberger --- safeweb/http.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/safeweb/http.go b/safeweb/http.go index c2787611e..d04a84bb5 100644 --- a/safeweb/http.go +++ b/safeweb/http.go @@ -301,6 +301,19 @@ func (s *Server) Serve(ln net.Listener) error { return s.h.Serve(ln) } +// ListenAndServe listens on the TCP network address addr and then calls Serve +// to handle requests on incoming connections. If addr == "", ":http" is used. +func (s *Server) ListenAndServe(addr string) error { + if addr == "" { + addr = ":http" + } + lst, err := net.Listen("tcp", addr) + if err != nil { + return err + } + return s.Serve(lst) +} + // Close closes all client connections and stops accepting new ones. func (s *Server) Close() error { return s.h.Close()