/
handler_health.go
74 lines (61 loc) · 2.16 KB
/
handler_health.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
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package irondb
import (
"context"
"net/http"
tctx "github.com/tricksterproxy/trickster/pkg/proxy/context"
"github.com/tricksterproxy/trickster/pkg/proxy/engines"
"github.com/tricksterproxy/trickster/pkg/proxy/headers"
"github.com/tricksterproxy/trickster/pkg/proxy/request"
"github.com/tricksterproxy/trickster/pkg/proxy/urls"
)
// HealthHandler checks the health of the Configured Upstream Origin
func (c *Client) HealthHandler(w http.ResponseWriter, r *http.Request) {
if c.healthURL == nil {
c.populateHeathCheckRequestValues()
}
if c.healthMethod == "-" {
w.WriteHeader(400)
w.Write([]byte("Health Check URL not Configured for origin: " + c.config.Name))
return
}
req, _ := http.NewRequest(c.healthMethod, c.healthURL.String(), nil)
rsc := request.GetResources(r)
req = req.WithContext(tctx.WithHealthCheckFlag(tctx.WithResources(context.Background(), rsc), true))
req.Header = c.healthHeaders.Clone()
engines.DoProxy(w, req, true)
}
func (c *Client) populateHeathCheckRequestValues() {
oc := c.config
if oc.HealthCheckUpstreamPath == "-" {
oc.HealthCheckUpstreamPath = "/" + mnState
}
if oc.HealthCheckVerb == "-" {
oc.HealthCheckVerb = http.MethodGet
}
if oc.HealthCheckQuery == "-" {
oc.HealthCheckQuery = ""
}
c.healthURL = urls.Clone(c.baseUpstreamURL)
c.healthURL.Path += oc.HealthCheckUpstreamPath
c.healthURL.RawQuery = oc.HealthCheckQuery
c.healthMethod = oc.HealthCheckVerb
if oc.HealthCheckHeaders != nil {
c.healthHeaders = http.Header{}
headers.UpdateHeaders(c.healthHeaders, oc.HealthCheckHeaders)
}
}