Per the conversation starting here: https://matrix.to/#/!alCakyySsFIAVfZLDL:matrix.org/$B_wrGA9l3SwDDuufbOxMw_VyrwH_Vg00coJDRQTG7_8?via=matrix.org&via=element.io&via=sw1v.org During test cleanup there's no need for us to try to gracefully shutdown---we're about to completely destroy the container anyway.
50 lines
875 B
Go
50 lines
875 B
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/complement/internal/config"
|
|
)
|
|
|
|
type Server struct {
|
|
URL string
|
|
Port int
|
|
server *http.Server
|
|
listener net.Listener
|
|
}
|
|
|
|
func NewServer(t *testing.T, comp *config.Complement, configFunc func(router *mux.Router)) *Server {
|
|
t.Helper()
|
|
|
|
listener, err := net.Listen("tcp", ":0")
|
|
if err != nil {
|
|
t.Fatalf("Could not create listener for web server: %s", err)
|
|
}
|
|
|
|
port := listener.Addr().(*net.TCPAddr).Port
|
|
|
|
r := mux.NewRouter()
|
|
|
|
configFunc(r)
|
|
|
|
server := &http.Server{Addr: ":0", Handler: r}
|
|
|
|
go server.Serve(listener)
|
|
|
|
return &Server{
|
|
URL: fmt.Sprintf("http://%s:%d", comp.HostnameRunningComplement, port),
|
|
Port: port,
|
|
server: server,
|
|
listener: listener,
|
|
}
|
|
}
|
|
|
|
func (s *Server) Close() {
|
|
s.server.Close()
|
|
s.listener.Close()
|
|
}
|