1
0

Fixed new code for HttpServer. Had to add net.Addr field to the struct to hold addr and network to use in Stop() method.

This commit is contained in:
Alexander Andreev 2023-08-06 03:06:24 +04:00
parent 7915091b96
commit 88b989bfbb
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -13,6 +13,7 @@ import (
type HttpServer struct {
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
}