Gin 实现代理

直接代理,不做任何修改

1
2
3
4
5
6
router.POST("/api", func(c *gin.Context) {
      proxy := httputil.ReverseProxy{Director: func(request *http.Request) {
		request = c.Request
	}}
	proxy.ServeHTTP(c.Writer, c.Request)
})

转发指定路径给后端服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
router.POST("/api/v1/backend", ReverseProxy())

func ReverseProxy() gin.HandlerFunc {
    host := "backend_server_host"

    return func(c *gin.Context) {
        uri := "http://" + host
        remote, err := url.Parse(uri) // backend server
        if err != nil {
        	log.Fatal(err)
        }
        proxy := httputil.NewSingleHostReverseProxy(remote)
        proxy.ServeHTTP(c.Writer, c.Request)
    }
}

将所有路径都转发

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
router.Any("/*path", ReverseProxy)

func ReverseProxy(c *gin.Context) {
    host := "backend_server_host"

    proxy := &httputil.ReverseProxy{}
    proxy.Director = func(req *http.Request) {
    	req.Header = c.Request.Header
    	req.Host = c.Request.Host // 使用客户端Host,避免301重定向问题
    	req.URL.Scheme = "http"
    	req.URL.Host = host
    	req.URL.Path = c.Param("path") // 请求路径地址,或者直接使用c.Request.URL.Path
    }
    proxy.ModifyResponse = func(resp *http.Response) error {
        // NOTE 修改响应给客户端的response, 或者用于统计响应时间
        return nil
    }	
    proxy.ServeHTTP(c.Writer, c.Request)
}

官方代理示例

 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
package main

import (
	"bufio"
	"log"
	"net/http"
	"net/url"

	"github.com/gin-gonic/gin"
)

const (
	// this is our reverse server ip address
	ReverseServerAddr = "127.0.0.1:2002"
)

// maybe we can have many real server addresses and do some load balanced strategy.
var RealAddr = []string{
	"http://127.0.0.1:2003",
}

// a fake function that we can do strategy here.
func getLoadBalanceAddr() string {
	return RealAddr[0]
}

func main() {
	r := gin.Default()
	r.GET("/:path", func(c *gin.Context) {
		// step 1: resolve proxy address, change scheme and host in requets
		req := c.Request
		proxy, err := url.Parse(getLoadBalanceAddr())
		if err != nil {
			log.Printf("error in parse addr: %v", err)
			c.String(500, "error")
			return
		}
		req.URL.Scheme = proxy.Scheme
		req.URL.Host = proxy.Host

		// step 2: use http.Transport to do request to real server.
		transport := http.DefaultTransport
		resp, err := transport.RoundTrip(req)
		if err != nil {
			log.Printf("error in roundtrip: %v", err)
			c.String(500, "error")
			return
		}

		// step 3: return real server response to upstream.
		for k, vv := range resp.Header {
			for _, v := range vv {
				c.Header(k, v)
			}
		}
		defer resp.Body.Close()
		bufio.NewReader(resp.Body).WriteTo(c.Writer)
		return
	})

	if err := r.Run(ReverseServerAddr); err != nil {
		log.Printf("Error: %v", err)
	}
}

参考