-
Notifications
You must be signed in to change notification settings - Fork 8
/
proxy.go
45 lines (39 loc) · 911 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
42
43
44
45
package main
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
)
type RequestDoer interface {
Do(req *http.Request) (*http.Response, error)
}
func NewProxyHandler(log *zerolog.Logger, client RequestDoer,
req RequestProcessor, resp ResponseProcessor) fiber.Handler {
proxy := &proxyHandler{log, client, req, resp}
return func(c *fiber.Ctx) error {
return proxy.Handle(c)
}
}
type proxyHandler struct {
log *zerolog.Logger
client RequestDoer
req RequestProcessor
resp ResponseProcessor
}
func (p *proxyHandler) Handle(c *fiber.Ctx) error {
var req *http.Request
defer func() {
if err := recover(); err != nil {
p.log.Error().Any("request", req).Msg("panic in proxy for request")
panic(err)
}
}()
req = p.req.Process(c)
resp, err := p.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
p.resp.Process(c, resp)
return nil
}