tun2: generalize the 502 page generation
This commit is contained in:
parent
e8cf973208
commit
141fbfed76
|
@ -433,21 +433,7 @@ func (s *Server) RemoveConn(connection *Connection) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoundTrip sends a HTTP request to a backend and then returns its response.
|
func gen502Page(req *http.Request) *http.Response {
|
||||||
func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
||||||
var conns []*Connection
|
|
||||||
|
|
||||||
val, ok := s.domains.Get(req.Host)
|
|
||||||
if ok {
|
|
||||||
conns, ok = val.([]*Connection)
|
|
||||||
if !ok {
|
|
||||||
ln.Error(ErrNoSuchBackend, ln.F{
|
|
||||||
"action": "no_backend_connected",
|
|
||||||
"remote": req.RemoteAddr,
|
|
||||||
"host": req.Host,
|
|
||||||
"uri": req.RequestURI,
|
|
||||||
})
|
|
||||||
|
|
||||||
template := `<html><head><title>no backends connected</title></head><body><h1>no backends connected</h1><p>Please ensure a backend is running for ${HOST}. This is request ID ${REQ_ID}.</p></body></html>`
|
template := `<html><head><title>no backends connected</title></head><body><h1>no backends connected</h1><p>Please ensure a backend is running for ${HOST}. This is request ID ${REQ_ID}.</p></body></html>`
|
||||||
|
|
||||||
resbody := []byte(os.Expand(template, func(in string) string {
|
resbody := []byte(os.Expand(template, func(in string) string {
|
||||||
|
@ -477,7 +463,25 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
Request: req,
|
Request: req,
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundTrip sends a HTTP request to a backend and then returns its response.
|
||||||
|
func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
var conns []*Connection
|
||||||
|
|
||||||
|
val, ok := s.domains.Get(req.Host)
|
||||||
|
if ok {
|
||||||
|
conns, ok = val.([]*Connection)
|
||||||
|
if !ok {
|
||||||
|
ln.Error(ErrNoSuchBackend, ln.F{
|
||||||
|
"action": "no_backend_connected",
|
||||||
|
"remote": req.RemoteAddr,
|
||||||
|
"host": req.Host,
|
||||||
|
"uri": req.RequestURI,
|
||||||
|
})
|
||||||
|
|
||||||
|
return gen502Page(req), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,7 +500,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
"uri": req.RequestURI,
|
"uri": req.RequestURI,
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil, ErrNoSuchBackend
|
return gen502Page(req), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c := goodConns[rand.Intn(len(goodConns))]
|
c := goodConns[rand.Intn(len(goodConns))]
|
||||||
|
|
Loading…
Reference in New Issue