Skip to content

Commit

Permalink
Add version handler for api
Browse files Browse the repository at this point in the history
  • Loading branch information
danyalprout authored and bitwiseguy committed Jun 19, 2024
1 parent ccdfbf9 commit f7c0f10
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.21.6-alpine3.19 as builder

RUN apk add --no-cache make gcc musl-dev linux-headers jq bash
RUN apk add --no-cache make gcc musl-dev linux-headers jq bash git

WORKDIR /app

Expand Down
3 changes: 3 additions & 0 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GITCOMMIT ?= $(shell git rev-parse HEAD)
LDFLAGS := -ldflags "-X github.com/base-org/blob-archiver/api/version.GitCommit=$(GITCOMMIT)"

blob-api:
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build -v $(LDFLAGS) -o ./bin/blob-api ./cmd/main.go

Expand Down
13 changes: 13 additions & 0 deletions api/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/attestantio/go-eth2-client/api"
"github.com/attestantio/go-eth2-client/spec/deneb"
m "github.com/base-org/blob-archiver/api/metrics"
"github.com/base-org/blob-archiver/api/version"
"github.com/base-org/blob-archiver/common/storage"
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -106,6 +107,7 @@ func NewAPI(dataStoreClient storage.DataStoreReader, beaconClient client.BeaconB
})

r.Get("/eth/v1/beacon/blob_sidecars/{id}", result.blobSidecarHandler)
r.Get("/eth/v1/node/version", result.versionHandler)

return result
}
Expand All @@ -128,6 +130,17 @@ func isKnownIdentifier(id string) bool {
return slices.Contains([]string{"genesis", "finalized", "head"}, id)
}

// versionHandler implements the /eth/v1/node/version endpoint.
func (a *API) versionHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", jsonAcceptType)
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(version.APIVersion)
if err != nil {
a.logger.Error("unable to encode version to JSON", "err", err)
errServerError.write(w)
}
}

// toBeaconBlockHash converts a string that can be a slot, hash or identifier to a beacon block hash.
func (a *API) toBeaconBlockHash(id string) (common.Hash, *httpError) {
if isHash(id) {
Expand Down
18 changes: 18 additions & 0 deletions api/service/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/base-org/blob-archiver/api/metrics"
"github.com/base-org/blob-archiver/api/version"
"github.com/base-org/blob-archiver/common/beacon/beacontest"
"github.com/base-org/blob-archiver/common/blobtest"
"github.com/base-org/blob-archiver/common/storage"
Expand Down Expand Up @@ -303,6 +304,23 @@ func TestAPIService(t *testing.T) {
}
}

func TestVersionHandler(t *testing.T) {
a, _, _, cleanup := setup(t)
defer cleanup()

request := httptest.NewRequest("GET", "/eth/v1/node/version", nil)
response := httptest.NewRecorder()

a.router.ServeHTTP(response, request)

require.Equal(t, 200, response.Code)
require.Equal(t, "application/json", response.Header().Get("Content-Type"))
var v version.Version
err := json.Unmarshal(response.Body.Bytes(), &v)
require.NoError(t, err)
require.Equal(t, "Blob Archiver API/unknown", v.Data.Version)
}

func TestHealthHandler(t *testing.T) {
a, _, _, cleanup := setup(t)
defer cleanup()
Expand Down
29 changes: 29 additions & 0 deletions api/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package version

import "fmt"

var (
GitCommit = ""
APIVersion Version
)

func init() {
commit := GitCommit
if commit == "" {
commit = "unknown"
}

APIVersion = Version{
Data: struct {
Version string `json:"version"`
}{
Version: fmt.Sprintf("Blob Archiver API/%s", commit),
},
}
}

type Version struct {
Data struct {
Version string `json:"version"`
} `json:"data"`
}
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
dockerfile: Dockerfile
env_file:
- .env
ports:
- "8000:8000"
command:
- "blob-api"
depends_on:
Expand Down

0 comments on commit f7c0f10

Please sign in to comment.