From 88b989bfbbd07cb3a58366470e4b1e26181fb4a2 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 6 Aug 2023 03:06:24 +0400 Subject: [PATCH] Fixed new code for HttpServer. Had to add net.Addr field to the struct to hold addr and network to use in Stop() method. --- internal/http/server.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/http/server.go b/internal/http/server.go index cba11e9..477e662 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -12,7 +12,8 @@ import ( ) type HttpServer struct { - s http.Server + s http.Server + addr net.Addr } func NewHttpServer(r http.Handler) *HttpServer { @@ -26,7 +27,6 @@ func (s *HttpServer) Start(address string) error { var network string if !strings.ContainsRune(address, ':') { network = "unix" - defer os.Remove(address) } else { ap, err := netip.ParseAddrPort(address) if err != nil { @@ -49,6 +49,8 @@ func (s *HttpServer) Start(address string) error { os.Chmod(address, 0777) } + s.addr = listener.Addr() + go func() { if err = s.s.Serve(listener); err != nil && err != http.ErrServerClosed { log.Fatalln(err) @@ -62,6 +64,10 @@ func (s *HttpServer) Stop() error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() + if s.addr.Network() == "unix" { + defer os.Remove(s.addr.String()) + } + if err := s.s.Shutdown(ctx); err != nil { return err }