forked from winkingzhang/goGate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (122 loc) · 3.09 KB
/
main.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
package main
import (
"html/template"
"net/http"
"net/http/httputil"
"log"
"fmt"
)
type ProxyMap struct {
Schema string `json:"schema"`
Host string `json:"host"`
Path string `json:"path"`
Headers *map[string]string `json:"headers"`
}
func NewProxyMap(schema, host, path string, headers *map[string]string) *ProxyMap {
return &ProxyMap{
Schema: schema,
Host: host,
Path: path,
Headers: headers,
}
}
type Site struct {
Backend *ProxyMap
reverseProxy *httputil.ReverseProxy
}
var sites = make(map[string]*Site)
func (s *Site) writePreLogging(r *http.Request) {
log.Printf("[reverse proxy] %v %v %v (%q) => [backend] %v://%v%v",
r.RemoteAddr, r.Method, r.Host, r.RequestURI,
s.Backend.Schema, s.Backend.Host, s.Backend.Path)
}
func (s *Site) getReverseProxy() *httputil.ReverseProxy {
if s.reverseProxy != nil {
return s.reverseProxy
}
return &httputil.ReverseProxy{
Director: func(r *http.Request) {
// set reverse proxy target
r.URL.Scheme = s.Backend.Schema
r.URL.Host = s.Backend.Host
r.URL.Path = s.Backend.Path
if _, ok := r.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
r.Header.Set("User-Agent", "")
}
// additional header
//r.Header.Set("X-Proxy-Secret", "Secret")
//r.Header.Set("Host", r.Host)
if s.Backend.Headers != nil {
for h, v := range *s.Backend.Headers {
r.Header.Set(h, v);
}
}
// prepare ok, write log and send request to backend
go s.writePreLogging(r)
},
}
}
func reverseProxyHandle(w http.ResponseWriter, r *http.Request) {
// parse requst url and create reverse proxy
site := sites[r.RequestURI]
if site != nil {
// pass to reversed proxy
site.getReverseProxy().ServeHTTP(w, r);
//go writePostLogging(w, r);
} else {
//make 404 error
http.NotFound(w, r);
}
}
func everythingHandle(w http.ResponseWriter, r *http.Request) {
log.Printf("[everythingHandle] reserved proxy > %v %v %v (%q)",
r.RemoteAddr, r.Method, r.Host, r.RequestURI)
switch r.Method {
case http.MethodGet:
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
</head>
<body>
<div><strong>Hello golang, this is {{.Name}}</strong></div>
</body>
</html>`
t, err := template.New("webpage").Parse(tpl)
if (err != nil) {
panic(err)
}
data := struct {
Title string
Name string
}{
Title: "API Gateway",
Name: "Gatway Home",
}
err = t.Execute(w, data);
if (err != nil) {
panic(err)
}
return
default:
http.NotFound(w, r)
return
}
}
func main() {
sites["/api/hello/v1"] = &Site{
Backend: NewProxyMap("http", "localhost:8081", "/api/hello", nil),
}
sites["/api/calc/v1"] = &Site{
Backend: NewProxyMap("http", "localhost:8082", "/api/calc", nil),
}
http.HandleFunc("/", everythingHandle)
http.HandleFunc("/api/hello/v1", reverseProxyHandle)
http.HandleFunc("/api/calc/v1", reverseProxyHandle)
fmt.Println("Server now listen on http://localhost:8080")
http.ListenAndServe(":8080", nil)
}