Skip to content

Commit ce340a9

Browse files
committed
Add version handler for api
1 parent 39cf8e7 commit ce340a9

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM golang:1.21.6-alpine3.19 as builder
22

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

55
WORKDIR /app
66

api/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
GITCOMMIT ?= $(shell git rev-parse HEAD)
2+
LDFLAGS := -ldflags "-X github.com/base-org/blob-archiver/api/version.GitCommit=$(GITCOMMIT)"
3+
14
blob-api:
25
env GO111MODULE=on GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) go build -v $(LDFLAGS) -o ./bin/blob-api ./cmd/main.go
36

api/service/api.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/attestantio/go-eth2-client/api"
1616
"github.com/attestantio/go-eth2-client/spec/deneb"
1717
m "github.com/base-org/blob-archiver/api/metrics"
18+
"github.com/base-org/blob-archiver/api/version"
1819
"github.com/base-org/blob-archiver/common/storage"
1920
opmetrics "github.com/ethereum-optimism/optimism/op-service/metrics"
2021
"github.com/ethereum/go-ethereum/common"
@@ -106,6 +107,7 @@ func NewAPI(dataStoreClient storage.DataStoreReader, beaconClient client.BeaconB
106107
})
107108

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

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

133+
// versionHandler implements the /eth/v1/node/version endpoint.
134+
func (a *API) versionHandler(w http.ResponseWriter, _ *http.Request) {
135+
w.Header().Set("Content-Type", jsonAcceptType)
136+
w.WriteHeader(http.StatusOK)
137+
err := json.NewEncoder(w).Encode(version.APIVersion)
138+
if err != nil {
139+
a.logger.Error("unable to encode version to JSON", "err", err)
140+
errServerError.write(w)
141+
}
142+
}
143+
131144
// toBeaconBlockHash converts a string that can be a slot, hash or identifier to a beacon block hash.
132145
func (a *API) toBeaconBlockHash(id string) (common.Hash, *httpError) {
133146
if isHash(id) {

api/service/api_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/attestantio/go-eth2-client/spec/deneb"
1616
"github.com/attestantio/go-eth2-client/spec/phase0"
1717
"github.com/base-org/blob-archiver/api/metrics"
18+
"github.com/base-org/blob-archiver/api/version"
1819
"github.com/base-org/blob-archiver/common/beacon/beacontest"
1920
"github.com/base-org/blob-archiver/common/blobtest"
2021
"github.com/base-org/blob-archiver/common/storage"
@@ -303,6 +304,23 @@ func TestAPIService(t *testing.T) {
303304
}
304305
}
305306

307+
func TestVersionHandler(t *testing.T) {
308+
a, _, _, cleanup := setup(t)
309+
defer cleanup()
310+
311+
request := httptest.NewRequest("GET", "/eth/v1/node/version", nil)
312+
response := httptest.NewRecorder()
313+
314+
a.router.ServeHTTP(response, request)
315+
316+
require.Equal(t, 200, response.Code)
317+
require.Equal(t, "application/json", response.Header().Get("Content-Type"))
318+
var v version.Version
319+
err := json.Unmarshal(response.Body.Bytes(), &v)
320+
require.NoError(t, err)
321+
require.Equal(t, "Blob Archiver API/unknown", v.Data.Version)
322+
}
323+
306324
func TestHealthHandler(t *testing.T) {
307325
a, _, _, cleanup := setup(t)
308326
defer cleanup()

api/version/version.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package version
2+
3+
import "fmt"
4+
5+
var (
6+
GitCommit = ""
7+
APIVersion Version
8+
)
9+
10+
func init() {
11+
commit := GitCommit
12+
if commit == "" {
13+
commit = "unknown"
14+
}
15+
16+
APIVersion = Version{
17+
Data: struct {
18+
Version string `json:"version"`
19+
}{
20+
Version: fmt.Sprintf("Blob Archiver API/%s", commit),
21+
},
22+
}
23+
}
24+
25+
type Version struct {
26+
Data struct {
27+
Version string `json:"version"`
28+
} `json:"data"`
29+
}

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ services:
77
dockerfile: Dockerfile
88
env_file:
99
- .env
10+
ports:
11+
- "8000:8000"
1012
command:
1113
- "blob-api"
1214
depends_on:

0 commit comments

Comments
 (0)