-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.go
122 lines (101 loc) · 3.06 KB
/
frontend.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"gopkg.in/tylerb/graceful.v1"
"gopkg.in/yaml.v2"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func NewRewriteReverseProxy(basePath string, redirectUrl string) *httputil.ReverseProxy {
target, err := url.Parse(redirectUrl)
if err != nil {
log.Fatal(err)
}
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = strings.TrimPrefix(req.URL.Path, basePath)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
}
return &httputil.ReverseProxy{Director: director}
}
type Config struct {
Routes map[string]string
}
type StatusLoggingResponseWriter struct {
status int
http.ResponseWriter
}
func NewStatusLoggingResponseWriter(res http.ResponseWriter) *StatusLoggingResponseWriter {
// Default the status code to 200.
return &StatusLoggingResponseWriter{200, res}
}
func (w *StatusLoggingResponseWriter) Status() int {
return w.status
}
// Satisfy the http.ResponseWriter interface.
func (w *StatusLoggingResponseWriter) Header() http.Header {
return w.ResponseWriter.Header()
}
func (w *StatusLoggingResponseWriter) Write(data []byte) (int, error) {
return w.ResponseWriter.Write(data)
}
func (w *StatusLoggingResponseWriter) WriteHeader(statusCode int) {
// Store the status code.
w.status = statusCode
// Write the status code onward.
w.ResponseWriter.WriteHeader(statusCode)
}
func NewLogrusHandler(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
loggingWriter := NewStatusLoggingResponseWriter(rw)
handler(loggingWriter, r)
latency := time.Since(start)
entry := log.WithFields(log.Fields{
"request": r.RequestURI,
"method": r.Method,
"remote": r.RemoteAddr,
"status": loggingWriter.Status(),
"text_status": http.StatusText(loggingWriter.Status()),
"latency": latency,
})
if reqID := r.Header.Get("X-Request-Id"); reqID != "" {
entry = entry.WithField("request_id", reqID)
}
entry.Info("completed handling request")
}
}
func NewCombinedHandler(handler func(http.ResponseWriter, *http.Request)) http.Handler {
return cors.Default().Handler(http.HandlerFunc(NewLogrusHandler(handler)))
}
func main() {
configFile, err := ioutil.ReadFile("config.yaml")
if err != nil {
panic(err)
}
var config Config
err = yaml.Unmarshal(configFile, &config)
if err != nil {
panic(err)
}
r := mux.NewRouter().StrictSlash(true)
// Create the reverse proxy paths specified in the config.
for base, redirectPath := range config.Routes {
proxy := NewRewriteReverseProxy(fmt.Sprintf("/%s", base), redirectPath)
r.NewRoute().PathPrefix(fmt.Sprintf("/%s/", base)).Handler(NewCombinedHandler(proxy.ServeHTTP))
}
graceful.Run(":8080", 10*time.Second, r)
}