From 130d4d944b5c57761b8f81b3c751cb27e2d904ae Mon Sep 17 00:00:00 2001 From: LexLuthr Date: Mon, 21 Nov 2022 18:08:16 +0530 Subject: [PATCH] introduce http api version --- cmd/booster-http/http_test.go | 22 ++++++++++++++++++++++ cmd/booster-http/server.go | 15 +++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/cmd/booster-http/http_test.go b/cmd/booster-http/http_test.go index dfd9204dc..efe4c151c 100644 --- a/cmd/booster-http/http_test.go +++ b/cmd/booster-http/http_test.go @@ -3,6 +3,7 @@ package main import ( "compress/gzip" "context" + "encoding/json" "io" "net/http" "os" @@ -156,3 +157,24 @@ func TestHttpResponseRedirects(t *testing.T) { err = httpServer.Stop() require.NoError(t, err) } + +func TestHttpInfo(t *testing.T) { + var v apiVersion + + // Create a new mock Http server + ctrl := gomock.NewController(t) + httpServer := NewHttpServer("", 7777, false, mocks_booster_http.NewMockHttpServerApi(ctrl)) + httpServer.Start(context.Background()) + + response, err := http.Get("http://localhost:7777/info") + require.NoError(t, err) + defer response.Body.Close() + + json.NewDecoder(response.Body).Decode(&v) //nolint:errcheck + require.Equal(t, "0.2.0", v.Version) + + // Stop the server + err = httpServer.Stop() + require.NoError(t, err) + +} diff --git a/cmd/booster-http/server.go b/cmd/booster-http/server.go index c628a4946..ca7b69d44 100644 --- a/cmd/booster-http/server.go +++ b/cmd/booster-http/server.go @@ -3,6 +3,7 @@ package main import ( "bufio" "context" + "encoding/json" "errors" "fmt" "io" @@ -43,6 +44,10 @@ const carSuffix = ".car" const pieceCidParam = "pieceCid" const payloadCidParam = "payloadCid" +type apiVersion struct { + Version string `json:"Version"` +} + type HttpServer struct { path string port int @@ -78,6 +83,7 @@ func (s *HttpServer) Start(ctx context.Context) { handler.HandleFunc(s.pieceBasePath(), s.handlePieceRequest) handler.HandleFunc("/", s.handleIndex) handler.HandleFunc("/index.html", s.handleIndex) + handler.HandleFunc("/info", s.handleInfo) handler.Handle("/metrics", metrics.Exporter("booster_http")) // metrics s.server = &http.Server{ Addr: listenAddr, @@ -199,6 +205,15 @@ func (s *HttpServer) handlePieceRequest(w http.ResponseWriter, r *http.Request) } } +func (s *HttpServer) handleInfo(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + v := apiVersion{ + Version: "0.2.0", + } + json.NewEncoder(w).Encode(v) //nolint:errcheck +} + func (s *HttpServer) handleByPayloadCid(payloadCid cid.Cid, isCar bool, w http.ResponseWriter, r *http.Request) { startTime := time.Now() ctx, span := tracing.Tracer.Start(r.Context(), "http.payload_cid")