make tests pass on base package.

This commit is contained in:
fiatjaf
2023-05-01 20:38:11 -03:00
parent a4512da371
commit e84f5df1f0
3 changed files with 42 additions and 74 deletions

View File

@@ -34,7 +34,6 @@ type Server struct {
// outputting to stderr.
Log Logger
addr string
relay Relay
// keep a connection reference to all connected clients for Server.Shutdown
@@ -42,6 +41,7 @@ type Server struct {
clients map[*websocket.Conn]struct{}
// in case you call Server.Start
Addr string
httpServer *http.Server
}
@@ -83,13 +83,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) Start(host string, port int) error {
func (s *Server) Start(host string, port int, started ...chan bool) error {
addr := net.JoinHostPort(host, strconv.Itoa(port))
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
s.Addr = ln.Addr().String()
s.httpServer = &http.Server{
Handler: cors.Default().Handler(s),
Addr: addr,
@@ -98,6 +99,11 @@ func (s *Server) Start(host string, port int) error {
IdleTimeout: 30 * time.Second,
}
// notify caller that we're starting
for _, started := range started {
close(started)
}
if err := s.httpServer.Serve(ln); err == http.ErrServerClosed {
return nil
} else if err != nil {