forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
41 lines (34 loc) · 857 Bytes
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package proxy
import (
"net/http"
"time"
gometrics "github.com/eBay/fabio/_third_party/github.com/rcrowley/go-metrics"
"github.com/eBay/fabio/config"
)
// Proxy is a dynamic reverse proxy.
type Proxy struct {
httpProxy http.Handler
wsProxy http.Handler
requests gometrics.Timer
}
func New(tr http.RoundTripper, cfg config.Proxy) *Proxy {
return &Proxy{
httpProxy: newHTTPProxy(tr, cfg),
wsProxy: newWSProxy(),
requests: gometrics.GetOrRegisterTimer("requests", gometrics.DefaultRegistry),
}
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if ShuttingDown() {
http.Error(w, "shutting down", http.StatusServiceUnavailable)
return
}
// http or ws?
h := p.httpProxy
if r.Header.Get("Upgrade") == "websocket" {
h = p.wsProxy
}
start := time.Now()
h.ServeHTTP(w, r)
p.requests.UpdateSince(start)
}