forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
178 lines (159 loc) · 4.63 KB
/
serve.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
package goss
import (
"bytes"
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"
"github.com/aelsabbahy/goss/outputs"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/system"
"github.com/aelsabbahy/goss/util"
"github.com/fatih/color"
"github.com/patrickmn/go-cache"
)
func Serve(c *util.Config) error {
endpoint := c.Endpoint
health, err := newHealthHandler(c)
if err != nil {
return err
}
http.Handle(endpoint, health)
log.Printf("Starting to listen on: %s", c.ListenAddress)
return http.ListenAndServe(c.ListenAddress, nil)
}
func newHealthHandler(c *util.Config) (*healthHandler, error) {
color.NoColor = true
cache := cache.New(c.Cache, 30*time.Second)
cfg, err := getGossConfig(c.Vars, c.VarsInline, c.Spec)
if err != nil {
return nil, err
}
output, err := getOutputer(c.NoColor, c.OutputFormat)
if err != nil {
return nil, err
}
health := &healthHandler{
c: c,
gossConfig: *cfg,
sys: system.New(c.PackageManager),
outputer: output,
cache: cache,
gossMu: &sync.Mutex{},
maxConcurrent: c.MaxConcurrent,
}
return health, nil
}
type res struct {
body bytes.Buffer
statusCode int
}
type healthHandler struct {
c *util.Config
gossConfig GossConfig
sys *system.System
outputer outputs.Outputer
cache *cache.Cache
gossMu *sync.Mutex
maxConcurrent int
}
func (h healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
outputFormat, outputer, err := h.negotiateResponseContentType(r)
if err != nil {
log.Printf("Warn: Using process-level output-format. %s", err)
outputFormat = h.c.OutputFormat
outputer = h.outputer
}
negotiatedContentType := h.responseContentType(outputFormat)
log.Printf("%v: requesting health probe", r.RemoteAddr)
resp := h.processAndEnsureCached(negotiatedContentType, outputer)
w.Header().Set(http.CanonicalHeaderKey("Content-Type"), negotiatedContentType)
w.WriteHeader(resp.statusCode)
logBody := ""
if resp.statusCode != http.StatusOK {
logBody = " - " + resp.body.String()
}
resp.body.WriteTo(w)
log.Printf("%v: status %d%s", r.RemoteAddr, resp.statusCode, logBody)
}
func (h healthHandler) processAndEnsureCached(negotiatedContentType string, outputer outputs.Outputer) res {
cacheKey := fmt.Sprintf("res:%s", negotiatedContentType)
tmp, found := h.cache.Get(cacheKey)
if found {
return tmp.(res)
}
h.gossMu.Lock()
defer h.gossMu.Unlock()
tmp, found = h.cache.Get(cacheKey)
if found {
log.Printf("Returning cached[%s].", cacheKey)
return tmp.(res)
}
log.Printf("Stale cache[%s], running tests", cacheKey)
resp := h.runValidate(outputer)
h.cache.SetDefault(cacheKey, resp)
return resp
}
func (h healthHandler) runValidate(outputer outputs.Outputer) res {
h.sys = system.New(h.c.PackageManager)
iStartTime := time.Now()
out := validate(h.sys, h.gossConfig, h.maxConcurrent)
var b bytes.Buffer
outputConfig := util.OutputConfig{
FormatOptions: h.c.FormatOptions,
}
exitCode := outputer.Output(&b, out, iStartTime, outputConfig)
resp := res{
body: b,
}
if exitCode == 0 {
resp.statusCode = http.StatusOK
} else {
resp.statusCode = http.StatusServiceUnavailable
}
return resp
}
const (
// https://en.wikipedia.org/wiki/Media_type
mediaTypePrefix = "application/vnd.goss-"
)
func (h healthHandler) negotiateResponseContentType(r *http.Request) (string, outputs.Outputer, error) {
acceptHeader := r.Header[http.CanonicalHeaderKey("Accept")]
var outputer outputs.Outputer
outputName := ""
for _, acceptCandidate := range acceptHeader {
acceptCandidate = strings.TrimSpace(acceptCandidate)
if strings.HasPrefix(acceptCandidate, mediaTypePrefix) {
outputName = strings.TrimPrefix(acceptCandidate, mediaTypePrefix)
} else if strings.EqualFold("application/json", acceptCandidate) || strings.EqualFold("text/json", acceptCandidate) {
outputName = "json"
} else {
outputName = ""
}
var err error
outputer, err = outputs.GetOutputer(outputName)
if err != nil {
continue
}
}
if outputer == nil {
return "", nil, fmt.Errorf("Accept header on request missing or invalid. Accept header: %v", acceptHeader)
}
return outputName, outputer, nil
}
func (h healthHandler) responseContentType(outputName string) string {
if outputName == "json" {
return "application/json"
}
return fmt.Sprintf("%s%s", mediaTypePrefix, outputName)
}
func (h healthHandler) renderBody(results <-chan []resource.TestResult, outputer outputs.Outputer) (int, bytes.Buffer) {
outputConfig := util.OutputConfig{
FormatOptions: h.c.FormatOptions,
}
var b bytes.Buffer
exitCode := outputer.Output(&b, results, time.Now(), outputConfig)
return exitCode, b
}