1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-06 21:34:19 -04:00
v2fly/testing/servers/http/http.go

41 lines
805 B
Go
Raw Normal View History

2016-01-24 15:48:38 -05:00
package tcp
import (
"net/http"
"v2ray.com/core/common/net"
2016-01-24 15:48:38 -05:00
)
type Server struct {
Port net.Port
2016-01-24 15:48:38 -05:00
PathHandler map[string]http.HandlerFunc
2017-11-08 19:06:40 -05:00
server *http.Server
2016-01-24 15:48:38 -05:00
}
2017-11-08 19:06:40 -05:00
func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
2016-01-24 15:48:38 -05:00
if req.URL.Path == "/" {
resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
resp.WriteHeader(http.StatusOK)
resp.Write([]byte("Home"))
return
}
2017-11-08 19:06:40 -05:00
handler, found := s.PathHandler[req.URL.Path]
2016-01-24 15:48:38 -05:00
if found {
handler(resp, req)
}
}
2017-11-08 19:06:40 -05:00
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
2016-01-24 15:48:38 -05:00
}
2018-02-08 09:39:46 -05:00
func (s *Server) Close() error {
return s.server.Close()
2016-01-24 15:48:38 -05:00
}