-
Notifications
You must be signed in to change notification settings - Fork 12
/
http.go
193 lines (169 loc) · 4.37 KB
/
http.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
package flywheel
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"text/template"
"time"
)
// Handler flywheel handler
type Handler struct {
Flywheel *Flywheel
tmpl *template.Template
// HTTPClient is the HTTP client to use when proxying request to the backends
// This is used to control redirect behavior.
HTTPClient *http.Client
}
// ErrIgnoreRedirects used for proxy redirect ignore
var ErrIgnoreRedirects = errors.New("Ignore Redirect Error")
// NewHandler create flywheel http handler
func NewHandler(fw *Flywheel) *Handler {
return &Handler{
Flywheel: fw,
HTTPClient: &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return ErrIgnoreRedirects
},
},
}
}
// sendPing - sends a request to the flywheel to retrieve/change the state
func (handler *Handler) sendPing(op string) Pong {
var err error
replyTo := make(chan Pong, 1)
sreq := Ping{replyTo: replyTo}
switch op {
case "start":
sreq.requestStart = true
case "stop":
sreq.requestStop = true
case "status":
sreq.noop = true
}
if strings.HasPrefix(op, "stop_in:") {
suffix := op[8:]
dur, e := time.ParseDuration(suffix)
if e != nil {
err = e
}
sreq.setTimeout = dur
}
handler.Flywheel.pings <- sreq
status := <-replyTo
if err != nil && status.Err == nil {
status.Err = err
}
return status
}
// TODO - refactor this function to use context
// TODO - add support for SSL
func (handler *Handler) proxy(w http.ResponseWriter, r *http.Request) {
r.URL.Host = handler.Flywheel.ProxyEndpoint(r.Host)
r.URL.Scheme = "http"
r.RequestURI = ""
r.URL.Query().Del("flywheel")
resp, err := handler.HTTPClient.Do(r)
if err != nil {
if urlError, ok := err.(*url.Error); ok && urlError.Err == ErrIgnoreRedirects {
err = nil
} else {
log.Print(err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
for key, value := range resp.Header {
w.Header()[key] = value
}
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
// if response code is between 300 and 400 sometimes body does not exist
if err != nil && (!(resp.StatusCode >= 300 && resp.StatusCode < 400)) {
log.Print(err)
}
}
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] %s %s", r.RemoteAddr, r.Method, r.RequestURI)
query := r.URL.Query()
param := query.Get("flywheel")
if param == "config" {
buf, err := json.MarshalIndent(handler.Flywheel.config, "", " ") // Might be unsafe, but this should be read only.
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
} else {
w.Header().Set("Content-Type", "application/json")
w.Write(buf)
}
return
}
pong := handler.sendPing(param)
if param == "start" {
query.Del("flywheel")
r.URL.RawQuery = query.Encode()
w.Header().Set("Location", r.URL.String())
w.WriteHeader(http.StatusTemporaryRedirect)
return
}
accept := query.Get("Accept")
var acceptHTML bool
if accept != "" {
htmlIndex := strings.Index(accept, "text/html")
jsonIndex := strings.Index(accept, "application/json")
if htmlIndex != -1 {
acceptHTML = jsonIndex == -1 || htmlIndex < jsonIndex
}
}
if param != "" {
buf, err := json.MarshalIndent(pong, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if param != "status" {
query.Del("flywheel")
r.URL.RawQuery = query.Encode()
w.Header().Set("Content-Type", "application/json")
if acceptHTML {
w.Header().Set("Location", r.URL.String())
w.WriteHeader(http.StatusTemporaryRedirect)
}
w.Write(buf)
} else {
w.Header().Set("Content-Type", "application/json")
w.Write(buf)
}
return
}
if pong.Err != nil {
body := fmt.Sprintf(HTMLERROR, pong.Err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(body))
return
}
switch pong.Status {
case STOPPED:
query.Set("flywheel", "start")
r.URL.RawQuery = query.Encode()
body := fmt.Sprintf(HTMLSTOPPED, r.URL)
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(body))
case STARTING:
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(HTMLSTARTING))
case STARTED:
handler.proxy(w, r)
case STOPPING:
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(HTMLSTOPPING))
case UNHEALTHY:
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(HTMLUNHEALTHY))
}
}