-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
197 lines (156 loc) · 3.9 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"fmt"
"github.com/my/repo/cache"
"github.com/my/repo/metricsExporter"
"github.com/my/repo/rateLimit"
"github.com/my/repo/util"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"unsafe"
)
type DataSet struct{
Status int
Body []byte
Header http.Header
}
type ProxyConfig struct {
schema string
host string
}
var (
cache *customCache.Redis
config *util.Config
rateLimiter *rateLimit.RateLimit
proxyConfig *ProxyConfig
exporter *metricsExporter.MetricsExporter
)
// proxyHandler handler
func proxyHandler(w http.ResponseWriter, r *http.Request) {
// delete path "/proxy"
r.RequestURI = strings.Replace(r.RequestURI, "/proxy", "", 1)
key := customCache.CreateKeyFromRequest(r)
// rate limit
if arrow := rateLimiter.Allow(); !arrow {
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
exporter.RateLimit(r.RequestURI)
return
}
var data DataSet
cr := cloneRequest(r)
if r.Method == "GET" {
// cache read only get request
if v, hit := cache.Get(key); hit {
exporter.Cache(r.RequestURI, "true")
writeBody(w, *(*[]byte)(unsafe.Pointer(&v)))
return
}
data = curl(cr)
cache.Set(key, *(*string)(unsafe.Pointer(&data.Body)), time.Second * 10)
exporter.Cache(r.RequestURI, "false")
} else {
data = curl(cr)
}
if data.Status >= http.StatusBadRequest {
http.Error(w, http.StatusText(data.Status), data.Status)
return
}
writeBody(w, data.Body)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
writeBody(w, []byte(http.StatusText(http.StatusOK)))
}
// overwrite client request host
func cloneRequest(r *http.Request) *http.Request {
b, e := ioutil.ReadAll(r.Body)
if e != nil {
log.Fatal(e.Error())
}
url := fmt.Sprintf("%s://%s%s", proxyConfig.schema, proxyConfig.host, r.RequestURI)
req, e:= http.NewRequest(r.Method, url, bytes.NewReader(b))
if e != nil {
log.Print(e.Error())
}
req.Header = make(http.Header)
for h, val := range r.Header {
req.Header[h] = val
}
return req
}
// http request
func curl(r *http.Request) DataSet {
var dataSet DataSet
res, e := (&http.Client {
// timeout is 1 sec
Timeout: time.Second * 1,
}).Do(r)
if e != nil {
log.Print(e.Error())
// TODO: this case return 400 ... ?
dataSet.Status = http.StatusBadRequest
return dataSet
}
defer res.Body.Close()
dataSet.Status = res.StatusCode
dataSet.Header = res.Header
var buf bytes.Buffer
_, e = io.Copy(&buf, res.Body)
if e != nil {
dataSet.Status = http.StatusInternalServerError
return dataSet
}
dataSet.Body = buf.Bytes()
return dataSet
}
func writeBody(w http.ResponseWriter, body []byte) {
w.WriteHeader(http.StatusOK)
if _, e := w.Write(body); e != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}
}
func main() {
// load config
config = util.ConfigLoad()
if config == nil {
panic("can't read config file...")
}
// set up redis
c, e := customCache.Init(customCache.Option {
Host: config.Redis.Host,
Port: config.Redis.Port,
Password: config.Redis.Password,
Db: config.Redis.No,
TimeoutMs: struct {
Read int
Write int
}{Read: config.Redis.TimeoutMs.Read, Write: config.Redis.TimeoutMs.Write},
})
if e != nil {
panic("can't listen redis server")
}
cache = c
// setup rate limit
rateLimiter = rateLimit.Init(config.Proxy.RateLimit)
// listen server
p := ":" + config.Server.Port
log.Println("server listen localhost" + p)
exporter = metricsExporter.Init()
// setup proxy config
proxyConfig = &ProxyConfig{
schema: config.Proxy.Target.Schema,
host: config.Proxy.Target.Host + ":" + config.Proxy.Target.Port,
}
http.HandleFunc("/proxy/", proxyHandler)
http.HandleFunc("/", healthHandler)
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(p, nil); err != nil {
panic(err.Error())
}
}