mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-05-06 01:27:05 +00:00
Add runtime info handler
This commit is contained in:
38
pkg/api/info.go
Normal file
38
pkg/api/info.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"github.com/stefanprodan/k8s-podinfo/pkg/version"
|
||||
)
|
||||
|
||||
func (s *Server) infoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := struct {
|
||||
Hostname string `json:"hostname"`
|
||||
Version string `json:"version"`
|
||||
Revision string `json:"revision"`
|
||||
Color string `json:"color"`
|
||||
Message string `json:"message"`
|
||||
GOOS string `json:"goos"`
|
||||
GOARCH string `json:"goarch"`
|
||||
Runtime string `json:"runtime"`
|
||||
NumGoroutine string `json:"num_goroutine"`
|
||||
NumCPU string `json:"num_cpu"`
|
||||
}{
|
||||
Hostname: s.config.Hostname,
|
||||
Version: version.VERSION,
|
||||
Revision: version.REVISION,
|
||||
Color: s.config.UIColor,
|
||||
Message: s.config.UIMessage,
|
||||
GOOS: runtime.GOOS,
|
||||
GOARCH: runtime.GOARCH,
|
||||
Runtime: runtime.Version(),
|
||||
NumGoroutine: strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
|
||||
NumCPU: strconv.FormatInt(int64(runtime.NumCPU()), 10),
|
||||
}
|
||||
|
||||
s.JSONResponse(w, r, data)
|
||||
}
|
||||
35
pkg/api/info_test.go
Normal file
35
pkg/api/info_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInfoHandler(t *testing.T) {
|
||||
req, err := http.NewRequest("GET", "/api/info", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
srv := NewMockServer()
|
||||
handler := http.HandlerFunc(srv.infoHandler)
|
||||
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check the response body is what we expect.
|
||||
expected := ".*color.*blue.*"
|
||||
r := regexp.MustCompile(expected)
|
||||
if !r.MatchString(rr.Body.String()) {
|
||||
t.Fatalf("handler returned unexpected body:\ngot \n%v \nwant \n%s",
|
||||
rr.Body.String(), expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user