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)
}
}
|