-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Accept: application/json on common HTTP endpoints (#2673)
* Added json support to rings, user stats, ha tracker Signed-off-by: Joe Elliott <[email protected]> * Added services template and accepts json support Signed-off-by: Joe Elliott <[email protected]> * lint + changelog Signed-off-by: Joe Elliott <[email protected]> * Improved json field names Signed-off-by: Joe Elliott <[email protected]> * Fixed accept header name and removed jpe notes Signed-off-by: Joe Elliott <[email protected]> * Added test Signed-off-by: Joe Elliott <[email protected]> * lint Signed-off-by: Joe Elliott <[email protected]> * Removed unused CSRF Token Placeholder Signed-off-by: Joe Elliott <[email protected]>
- Loading branch information
1 parent
58790db
commit 80b1261
Showing
7 changed files
with
178 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,71 @@ | ||
package cortex | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/util" | ||
) | ||
|
||
const tpl = ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Cortex Services Status</title> | ||
</head> | ||
<body> | ||
<h1>Cortex Services Status</h1> | ||
<p>Current time: {{ .Now }}</p> | ||
<table border="1"> | ||
<thead> | ||
<tr> | ||
<th>Service</th> | ||
<th>Status</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{{ range .Services }} | ||
<tr> | ||
<td>{{ .Name }}</td> | ||
<td>{{ .Status }}</td> | ||
</tr> | ||
{{ end }} | ||
</tbody> | ||
</table> | ||
</body> | ||
</html>` | ||
|
||
var tmpl *template.Template | ||
|
||
type renderService struct { | ||
Name string `json:"name"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func init() { | ||
tmpl = template.Must(template.New("webpage").Parse(tpl)) | ||
} | ||
|
||
func (t *Cortex) servicesHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(200) | ||
w.Header().Set("Content-Type", "text/plain") | ||
|
||
// TODO: this could be extended to also print sub-services, if given service has any | ||
svcs := make([]renderService, 0) | ||
for mod, s := range t.ServiceMap { | ||
if s != nil { | ||
fmt.Fprintf(w, "%v => %v\n", mod, s.State()) | ||
} | ||
svcs = append(svcs, renderService{ | ||
Name: mod, | ||
Status: s.State().String(), | ||
}) | ||
} | ||
|
||
// TODO: this could be extended to also print sub-services, if given service has any | ||
util.RenderHTTPResponse(w, struct { | ||
Now time.Time `json:"now"` | ||
Services []renderService `json:"services"` | ||
}{ | ||
Now: time.Now(), | ||
Services: svcs, | ||
}, tmpl, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package util | ||
|
||
import ( | ||
"html/template" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRenderHTTPResponse(t *testing.T) { | ||
type testStruct struct { | ||
Name string `json:"name"` | ||
Value int `json:"value"` | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
headers map[string]string | ||
tmpl string | ||
expected string | ||
value testStruct | ||
}{ | ||
{ | ||
name: "Test Renders json", | ||
headers: map[string]string{ | ||
"Accept": "application/json", | ||
}, | ||
tmpl: "<html></html>", | ||
expected: `{"name":"testName","value":42}`, | ||
value: testStruct{ | ||
Name: "testName", | ||
Value: 42, | ||
}, | ||
}, | ||
{ | ||
name: "Test Renders html", | ||
headers: map[string]string{}, | ||
tmpl: "<html>{{ .Name }}</html>", | ||
expected: "<html>testName</html>", | ||
value: testStruct{ | ||
Name: "testName", | ||
Value: 42, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tmpl := template.Must(template.New("webpage").Parse(tt.tmpl)) | ||
writer := httptest.NewRecorder() | ||
request := httptest.NewRequest("GET", "/", nil) | ||
|
||
for k, v := range tt.headers { | ||
request.Header.Add(k, v) | ||
} | ||
|
||
RenderHTTPResponse(writer, tt.value, tmpl, request) | ||
|
||
assert.Equal(t, tt.expected, writer.Body.String()) | ||
}) | ||
} | ||
} |