/
main.go
60 lines (48 loc) · 1.31 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
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"github.com/emicklei/go-restful"
)
func main() {
c := restful.NewContainer()
ws := new(restful.WebService)
ws.
Path("/").
Doc("API Gateway")
ws.Route(ws.GET("/").To(gateway).
Doc("gateway function").
Operation("gateway"))
ws.Route(ws.GET("/{asdf}").To(gateway).
Operation("gateway"))
ws.Route(ws.GET("/{asdf}/{asdf}").To(gateway).
Operation("gateway"))
c.Add(ws)
server := &http.Server{Addr: "localhost:8080", Handler: c}
log.Printf("start listening on localhost:8080")
log.Fatal(server.ListenAndServe())
}
func gateway(request *restful.Request, response *restful.Response) {
log.Printf(request.HeaderParameter("API-Version"))
if request.HeaderParameter("API-Version") == "1.0" {
target, err := url.Parse("http://localhost:8008")
if err != nil {
log.Print(err)
return
}
c := httputil.NewSingleHostReverseProxy(target)
c.ServeHTTP(response.ResponseWriter, request.Request)
}
if request.HeaderParameter("API-Version") == "1.1" {
target, err := url.Parse("http://localhost:8008")
if err != nil {
log.Print(err)
return
}
c := httputil.NewSingleHostReverseProxy(target)
c.ServeHTTP(response.ResponseWriter, request.Request)
}
response.WriteErrorString(http.StatusNotFound, "404: Version not found!.")
}