Skip to content

Commit

Permalink
endpoint for version control (#892)
Browse files Browse the repository at this point in the history
* endpoint for version control
---------

Signed-off-by: Stanislav Viyachev <[email protected]>
  • Loading branch information
Coiling-Dragon authored Apr 28, 2023
1 parent 56368b7 commit a9e705e
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app/router/validator-version/validator-version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 LimeChain Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package validator_version

import (
"github.com/go-chi/chi"
"github.com/go-chi/render"
"net/http"
"os"
)

type VersionResponse struct {
Version string `json:"version"`
}

var (
Route = "/version"
)

// Router for version check
func NewRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", versionResponse())
return r
}

// GET: .../version
func versionResponse() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
version := os.Getenv("VERSION_TAG")
render.JSON(w, r, &VersionResponse{Version: version})
}
}
72 changes: 72 additions & 0 deletions app/router/validator-version/validator_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 LimeChain Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package validator_version

import (
"bytes"
"encoding/json"
"fmt"
// "net/http/httptest"

"net/http"
"os"
"testing"

"github.com/limechain/hedera-eth-bridge-validator/test/mocks"
"github.com/stretchr/testify/assert"
)

func Test_NewRouter(t *testing.T) {
router := NewRouter()

assert.NotNil(t, router)
}

func Test_versionResponse(t *testing.T) {
mocks.Setup()

os.Setenv("VERSION_TAG", "1.0.0")

buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)

versionResp := &VersionResponse{
Version: "1.0.0",
}

var err error
if err := enc.Encode(versionResp); err != nil {
t.Fatalf("Failed to encode response for ResponseWriter. Err: [%s]", err.Error())
}

versionResponseAsBytes := buf.Bytes()
mocks.MResponseWriter.On("Header").Return(http.Header{})
mocks.MResponseWriter.On("Write", versionResponseAsBytes).Return(len(versionResponseAsBytes), nil)

versionResponseHandler := versionResponse()
versionResponseHandler(mocks.MResponseWriter, new(http.Request))

var versionResponse VersionResponse
err = json.Unmarshal(versionResponseAsBytes, &versionResponse)
if err != nil {
fmt.Println("Error:", err)
return
}

assert.Equal(t, &versionResponse, versionResp)
assert.Equal(t, versionResponse.Version, "1.0.0")
}
2 changes: 2 additions & 0 deletions bootstrap/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/limechain/hedera-eth-bridge-validator/app/router/transfer"
"github.com/limechain/hedera-eth-bridge-validator/app/router/transfer-reset"
"github.com/limechain/hedera-eth-bridge-validator/app/router/utils"
"github.com/limechain/hedera-eth-bridge-validator/app/router/validator-version"
"github.com/limechain/hedera-eth-bridge-validator/config"
"github.com/limechain/hedera-eth-bridge-validator/config/parser"
"github.com/limechain/hedera-eth-bridge-validator/constants"
Expand All @@ -45,5 +46,6 @@ func InitializeAPIRouter(services *Services, bridgeConfig *parser.Bridge, nodeCo
apiRouter.AddV1Router(utils.Route, utils.NewRouter(services.Utils))
apiRouter.AddV1Router(fees.Route, fees.NewRouter(services.Pricing))
apiRouter.AddV1Router(transfer_reset.Route, transfer_reset.NewRouter(services.transfers, services.Prometheus, nodeConfig))
apiRouter.AddV1Router(validator_version.Route, validator_version.NewRouter())
return apiRouter
}
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
validator:
image: gcr.io/hedera-eth-bridge-test/hedera-eth-bridge-validator:${TAG}
environment:
VERSION_TAG: ${TAG}
VALIDATOR_DATABASE_HOST: db
restart: always
tty: true
Expand Down
3 changes: 3 additions & 0 deletions examples/three-validators/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ services:
context: ../..
dockerfile: ./build/Dockerfile
environment:
VERSION_TAG: ${TAG}
VALIDATOR_DATABASE_HOST: alice_db
volumes:
- ./bridge.yml:/src/hedera-eth-bridge-validator/config/bridge.yml
Expand All @@ -79,6 +80,7 @@ services:
bob:
image: eth-hedera-validator
environment:
VTAG: ${TAG}
VALIDATOR_DATABASE_HOST: bob_db
volumes:
- ./bridge.yml:/src/hedera-eth-bridge-validator/config/bridge.yml
Expand All @@ -92,6 +94,7 @@ services:
carol:
image: eth-hedera-validator
environment:
VTAG: ${TAG}
VALIDATOR_DATABASE_HOST: carol_db
volumes:
- ./bridge.yml:/src/hedera-eth-bridge-validator/config/bridge.yml
Expand Down

0 comments on commit a9e705e

Please sign in to comment.