Skip to content

Commit

Permalink
fix #2206 add http handler to display version string (#2207)
Browse files Browse the repository at this point in the history
Signed-off-by: nitram509 <[email protected]>
  • Loading branch information
nitram509 authored Oct 21, 2024
1 parent 95ebb06 commit 0b6f4a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/guacgql/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"github.com/guacsec/guac/pkg/version"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -101,6 +102,7 @@ func startServer(cmd *cobra.Command) {
}

http.HandleFunc("/healthz", healthHandler)
http.HandleFunc("/version", versionHandler)

http.Handle("/query", srvHandler)
proto := "http"
Expand Down Expand Up @@ -191,6 +193,12 @@ func healthHandler(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "Server is healthy")
}

func versionHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, version.Version)
}

func getArango(_ context.Context) backends.BackendArgs {
return &arangodb.ArangoConfig{
User: flags.arangoUser,
Expand Down
27 changes: 27 additions & 0 deletions cmd/guacgql/cmd/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"github.com/guacsec/guac/pkg/version"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)

func Test_versionHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(versionHandler))
defer ts.Close()

res, err := http.Get(ts.URL)
assert.NoError(t, err)

actualData, err := io.ReadAll(res.Body)
assert.NoError(t, err)

err = res.Body.Close()
assert.NoError(t, err)

actualStr := string(actualData)
assert.Equal(t, version.Version, actualStr)
}

0 comments on commit 0b6f4a9

Please sign in to comment.