-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.go
68 lines (58 loc) · 1.84 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package proxyKit
import (
"github.com/gin-gonic/gin"
"github.com/richelieu-yang/chimera/v3/src/component/web/httpKit"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"net/http"
"net/http/httputil"
"net/url"
)
// Proxy 代理请求(反向代理,请求转发).
/*
调用此方法就行请求转发前,按照实际场景可以:
(1) POST请求,覆盖 request body;
(2) 变更请求头(request header).
@param w e.g.ctx.Writer
@param r e.g.ctx.Request
@param host e.g."127.0.0.1:12345"
@param options optional
*/
func Proxy(w http.ResponseWriter, r *http.Request, host string, options ...ProxyOption) error {
opts := loadOptions(options...)
return opts.proxy(w, r, host)
}
func ProxyWithGin(ctx *gin.Context, host string, options ...ProxyOption) error {
opts := loadOptions(options...)
opts.ctx = ctx
return opts.proxy(ctx.Writer, ctx.Request, host)
}
// ProxyToUrl
/*
项目实战:用 Go 创建一个简易负载均衡器
https://mp.weixin.qq.com/s/pe0CQa3tdrUmC86OSRBNeg
@param targetUrl 可以是 url.Parse() 的返回值
*/
func ProxyToUrl(w http.ResponseWriter, r *http.Request, targetUrl *url.URL) (err error) {
/* reset Request.Body */
if err = httpKit.TryToResetRequestBody(r); err != nil {
return
}
/* Richelieu: 在请求头加个标记,证明此请求被 chimera 代理过 */
mark(r.Header)
reverseProxy := httputil.NewSingleHostReverseProxy(targetUrl)
reverseProxy.ErrorHandler = func(writer http.ResponseWriter, r *http.Request, err1 error) {
err = errorKit.Wrapf(err1, "fail to proxy")
}
// Richelieu: 此处的 recover() 是针对 ReverseProxy.ServeHTTP() 中的 panic(http.ErrAbortHandler)
defer func() {
if obj := recover(); obj != nil {
if err1, ok := obj.(error); ok {
err = err1
return
}
err = errorKit.Newf("recover from %v", obj)
}
}()
reverseProxy.ServeHTTP(w, r)
return
}