Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce http api version #972

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cmd/booster-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"compress/gzip"
"context"
"encoding/json"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -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)

}
15 changes: 15 additions & 0 deletions cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down