server: listen on socket provided by system(f)d, if available
Alan Pearce alan@alanpearce.eu
Thu, 13 Jun 2024 17:08:33 +0200
3 files changed, 12 insertions(+), 2 deletions(-)
M internal/server/server.go → internal/server/server.go
@@ -6,6 +6,7 @@ "fmt" "net" "net/http" "net/url" + "os" "slices" "time" @@ -145,7 +146,15 @@ }, nil } func (s *Server) Start() error { - if err := s.ListenAndServe(); err != http.ErrServerClosed { + f := os.NewFile(uintptr(3), "") + l, err := net.FileListener(f) + if err != nil { + l, err = net.Listen("tcp", s.Addr) + if err != nil { + return errors.Wrap(err, "could not create listener") + } + } + if err := http.Serve(l, s.Handler); err != http.ErrServerClosed { return errors.Wrap(err, "error creating/closing server") }