-
Notifications
You must be signed in to change notification settings - Fork 17
/
proxy.go
40 lines (29 loc) · 609 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
package templar
import (
"io"
"net/http"
"time"
)
type Proxy struct {
client Client
stats Stats
}
func NewProxy(cl Client, stats Stats) *Proxy {
return &Proxy{cl, stats}
}
type copyResonder struct {
w http.ResponseWriter
}
func (c *copyResonder) Send(res *http.Response) io.Writer {
for k, v := range res.Header {
c.w.Header()[k] = v
}
c.w.WriteHeader(res.StatusCode)
return c.w
}
func (p *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
start := time.Now()
p.stats.StartRequest(req)
p.client.Forward(©Resonder{res}, req)
p.stats.Emit(req, time.Since(start))
}