feat(api): add /version endpoint

Fixes #3332
This commit is contained in:
Łukasz Mierzwa
2021-07-19 18:14:48 +01:00
committed by Łukasz Mierzwa
parent 875d13d044
commit 434eb677fd
4 changed files with 30 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
- Added a dedicated API endpoint for overview modal.
- Individual alert details are now lazy-loaded to improve performance
when dealing with a huge number of alerts per group.
- Added `/version` endpoint returning karma and Go runtime version #3332.
### Changed

View File

@@ -114,6 +114,7 @@ func setupRouter(router *chi.Mux, historyPoller *historyPoller) {
allowAuthBypass := []string{
getViewURL("/health"),
getViewURL("/metrics"),
getViewURL("/version"),
}
if config.Config.Authentication.Header.Name != "" {
config.Config.Authentication.Enabled = true
@@ -131,6 +132,7 @@ func setupRouter(router *chi.Mux, historyPoller *historyPoller) {
router.Get(getViewURL(""), redirectIndex)
}
router.Get(getViewURL("/"), index)
router.Get(getViewURL("/version"), versionHandler)
router.Get(getViewURL("/health"), pong)
router.Get(getViewURL("/robots.txt"), robots)
router.Get(getViewURL("/metrics"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"runtime"
"sort"
"strings"
"time"
@@ -39,6 +40,20 @@ func badRequestJSON(w http.ResponseWriter, error string) {
_, _ = w.Write(out)
}
type KarmaVersion struct {
Version string `json:"version"`
Golang string `json:"golang"`
}
func versionHandler(w http.ResponseWriter, r *http.Request) {
ver := KarmaVersion{
Version: version,
Golang: runtime.Version(),
}
data, _ := json.MarshalIndent(ver, "", " ")
_, _ = w.Write(data)
}
func pong(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Pong\n"))
}

View File

@@ -89,6 +89,18 @@ func TestRobots(t *testing.T) {
}
}
func TestVersion(t *testing.T) {
mockConfig()
r := testRouter()
setupRouter(r, nil)
req := httptest.NewRequest("GET", "/version", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("GET /version returned status %d", resp.Code)
}
}
func TestHealthPrefix(t *testing.T) {
os.Setenv("LISTEN_PREFIX", "/prefix")
defer os.Unsetenv("LISTEN_PREFIX")