Browse Source

close http server properly

pull/700/head
Darien Raymond 7 years ago
parent
commit
6e61538b36
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 19
      testing/servers/http/http.go

19
testing/servers/http/http.go

@ -10,9 +10,10 @@ type Server struct {
Port net.Port
PathHandler map[string]http.HandlerFunc
accepting bool
server *http.Server
}
func (server *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
resp.WriteHeader(http.StatusOK)
@ -20,17 +21,21 @@ func (server *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return
}
handler, found := server.PathHandler[req.URL.Path]
handler, found := s.PathHandler[req.URL.Path]
if found {
handler(resp, req)
}
}
func (server *Server) Start() (net.Destination, error) {
go http.ListenAndServe("127.0.0.1:"+server.Port.String(), server)
return net.TCPDestination(net.LocalHostIP, net.Port(server.Port)), nil
func (s *Server) Start() (net.Destination, error) {
s.server = &http.Server{
Addr: "127.0.0.1:" + s.Port.String(),
Handler: s,
}
go s.server.ListenAndServe()
return net.TCPDestination(net.LocalHostIP, net.Port(s.Port)), nil
}
func (v *Server) Close() {
v.accepting = false
func (s *Server) Close() {
s.server.Close()
}

Loading…
Cancel
Save