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

Added group for about info endpoint #332

Merged
merged 8 commits into from
Oct 14, 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
2 changes: 1 addition & 1 deletion .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
dep ensure
fi
- name: Build
run: cd cmd/proxy/ && go build -v .
run: make build
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
SHELL := $(shell which bash)

.DEFAULT_GOAL := help

.PHONY: clean-test test build run

help:
@echo -e ""
@echo -e "Make commands:"
@grep -E '^[a-zA-Z_-]+:.*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":"}; {printf "\t\033[36m%-30s\033[0m\n", $$1}'
@echo -e ""

# #########################
# Base commands
# #########################

cmd_dir = cmd/proxy
binary = proxy

clean-test:
go clean -testcache ./...

test: clean-test
go test ./...

build:
cd ${cmd_dir} && \
go build -v \
-o ${binary} \
-ldflags="-X main.appVersion=$(shell git describe --tags --long --dirty) -X main.commitID=$(shell git rev-parse HEAD)"

run: build
cd ${cmd_dir} && \
./${binary} --log-level="*:DEBUG"
6 changes: 6 additions & 0 deletions api/apiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func initBaseGroupsWithFacade(facade data.FacadeHandler) (map[string]data.GroupH
return nil, err
}

aboutGroup, err := groups.NewAboutGroup(facade)
if err != nil {
return nil, err
}

return map[string]data.GroupHandler{
"/actions": actionsGroup,
"/address": accountsGroup,
Expand All @@ -114,6 +119,7 @@ func initBaseGroupsWithFacade(facade data.FacadeHandler) (map[string]data.GroupH
"/validator": validatorsGroup,
"/vm-values": vmValuesGroup,
"/proof": proofGroup,
"/about": aboutGroup,
}, nil
}

Expand Down
43 changes: 43 additions & 0 deletions api/groups/baseAboutGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package groups

import (
"net/http"

"github.com/ElrondNetwork/elrond-proxy-go/api/shared"
"github.com/ElrondNetwork/elrond-proxy-go/data"
"github.com/gin-gonic/gin"
)

type aboutGroup struct {
facade AboutFacadeHandler
*baseGroup
}

// NewAboutGroup returns a new instance of aboutGroup
func NewAboutGroup(facadeHandler data.FacadeHandler) (*aboutGroup, error) {
facade, ok := facadeHandler.(AboutFacadeHandler)
if !ok {
return nil, ErrWrongTypeAssertion
}
ag := &aboutGroup{
facade: facade,
baseGroup: &baseGroup{},
}

baseRoutesHandlers := []*data.EndpointHandlerData{
{Path: "", Handler: ag.getAboutInfo, Method: http.MethodGet},
}
ag.baseGroup.endpoints = baseRoutesHandlers

return ag, nil
}

func (ag *aboutGroup) getAboutInfo(c *gin.Context) {
aboutInfo, err := ag.facade.GetAboutInfo()
if err != nil {
shared.RespondWith(c, http.StatusInternalServerError, nil, err.Error(), data.ReturnCodeInternalError)
return
}

c.JSON(http.StatusOK, aboutInfo)
}
71 changes: 71 additions & 0 deletions api/groups/baseAboutGroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package groups_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/ElrondNetwork/elrond-proxy-go/api/groups"
"github.com/ElrondNetwork/elrond-proxy-go/api/mock"
"github.com/ElrondNetwork/elrond-proxy-go/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type aboutResponse struct {
Data data.AboutInfo `json:"data"`
Error string `json:"error"`
Code string `json:"code"`
}

func TestNewAboutGroup(t *testing.T) {
t.Parallel()

t.Run("wrong facade, should fail", func(t *testing.T) {
t.Parallel()

wrongFacade := &mock.WrongFacade{}
group, err := groups.NewAboutGroup(wrongFacade)
require.Nil(t, group)
require.Equal(t, groups.ErrWrongTypeAssertion, err)
})

t.Run("should work", func(t *testing.T) {
t.Parallel()

group, err := groups.NewAboutGroup(&mock.Facade{})
require.Nil(t, err)
require.NotNil(t, group)
})
}

func TestAboutGroup_GetAboutInfo(t *testing.T) {
t.Parallel()

commitID := "commitID"
version := "appVersion"

facade := &mock.Facade{
GetAboutInfoCalled: func() (*data.GenericAPIResponse, error) {
return &data.GenericAPIResponse{
Data: data.AboutInfo{AppVersion: version, CommitID: commitID},
}, nil
},
}
aboutGroup, err := groups.NewAboutGroup(facade)
require.NoError(t, err)

ws := startProxyServer(aboutGroup, "/about")

req, _ := http.NewRequest("GET", "/about", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

apiResp := aboutResponse{}
loadResponse(resp.Body, &apiResp)

assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, apiResp.Data.AppVersion, version)
assert.Equal(t, apiResp.Data.CommitID, commitID)
assert.Empty(t, apiResp.Error)
}
5 changes: 5 additions & 0 deletions api/groups/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,8 @@ type ActionsFacadeHandler interface {
ReloadObservers() data.NodesReloadResponse
ReloadFullHistoryObservers() data.NodesReloadResponse
}

// AboutFacadeHandler defines the methods that can be used from the facade
type AboutFacadeHandler interface {
GetAboutInfo() (*data.GenericAPIResponse, error)
}
6 changes: 6 additions & 0 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Facade struct {
GetGenesisNodesPubKeysCalled func() (*data.GenericAPIResponse, error)
GetGasConfigsCalled func() (*data.GenericAPIResponse, error)
IsOldStorageForTokenCalled func(tokenID string, nonce uint64) (bool, error)
GetAboutInfoCalled func() (*data.GenericAPIResponse, error)
}

// GetProof -
Expand Down Expand Up @@ -464,6 +465,11 @@ func (f *Facade) GetGasConfigs() (*data.GenericAPIResponse, error) {
return f.GetGasConfigsCalled()
}

// GetAboutInfo -
func (f *Facade) GetAboutInfo() (*data.GenericAPIResponse, error) {
return f.GetAboutInfoCalled()
}

// WrongFacade is a struct that can be used as a wrong implementation of the node router handler
type WrongFacade struct {
}
5 changes: 5 additions & 0 deletions cmd/proxy/config/apiConfig/v1_0.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
# RateLimit: if set to 0, then the endpoint won't be limited. Otherwise, a given IP address can only make a number of
# requests in a given time stamp, configurable in config.toml

[APIPackages.about]
Routes = [
{ Name = "", Open = true, Secured = false, RateLimit = 0 },
]

[APIPackages.actions]
Routes = [
{ Name = "/reload-observers", Open = true, Secured = true, RateLimit = 0 },
Expand Down
5 changes: 5 additions & 0 deletions cmd/proxy/config/apiConfig/v_next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
# RateLimit: if set to 0, then the endpoint won't be limited. Otherwise, a given IP address can only make a number of
# requests in a given time stamp, configurable in config.toml

[APIPackages.about]
Routes = [
{ Name = "", Open = true, Secured = false, RateLimit = 0 },
]

[APIPackages.actions]
Routes = [
{ Name = "/reload-observers", Open = true, Secured = true, RateLimit = 0 },
Expand Down
18 changes: 18 additions & 0 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
erdConfig "github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-proxy-go/api"
"github.com/ElrondNetwork/elrond-proxy-go/common"
"github.com/ElrondNetwork/elrond-proxy-go/config"
"github.com/ElrondNetwork/elrond-proxy-go/data"
"github.com/ElrondNetwork/elrond-proxy-go/metrics"
Expand All @@ -40,6 +41,17 @@ const (
logFileMaxSizeInMB = 1024
)

// commitID and appVersion should be populated at build time using ldflags
//
// Usage examples:
// linux/mac:
// go build -i -v -ldflags="-X main.appVersion=$(git describe --tags --long --dirty) -X main.commitID=$(git rev-parse HEAD)"
// windows:
// for /f %i in ('git describe --tags --long --dirty') do set VERS=%i
// go build -i -v -ldflags="-X main.appVersion=%VERS%"
var commitID = common.UndefinedCommitString
var appVersion = common.UnVersionedAppString

var (
memoryBallastObject []byte
proxyHelpTemplate = `NAME:
Expand Down Expand Up @@ -537,6 +549,11 @@ func createVersionsRegistry(
return nil, err
}

aboutInfoProc, err := process.NewAboutProcessor(appVersion, commitID)
if err != nil {
return nil, err
}

facadeArgs := versionsFactory.FacadeArgs{
ActionsProcessor: bp,
AccountProcessor: accntProc,
Expand All @@ -552,6 +569,7 @@ func createVersionsRegistry(
PubKeyConverter: pubKeyConverter,
ESDTSuppliesProcessor: esdtSuppliesProc,
StatusProcessor: statusProc,
AboutInfoProcessor: aboutInfoProc,
}

apiConfigParser, err := versionsFactory.NewApiConfigParser(apiConfigDirectoryPath)
Expand Down
6 changes: 6 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package common

// UnVersionedAppString defines the default string, when the binary was build without setting the app flag
const UnVersionedAppString = "undefined"

// UndefinedCommitString defines the default string, when the binary was build without setting the commit flag
const UndefinedCommitString = "undefined"

// OutputFormat represents the format type returned by api
type OutputFormat uint8

Expand Down
7 changes: 7 additions & 0 deletions data/aboutInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package data

// AboutInfo defines the structure needed for exposing app info
type AboutInfo struct {
AppVersion string `json:"appVersion"`
CommitID string `json:"commitID"`
}
11 changes: 11 additions & 0 deletions facade/baseFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ElrondProxyFacade struct {
statusProc StatusProcessor

pubKeyConverter core.PubkeyConverter
aboutInfoProc AboutInfoProcessor
}

// NewElrondProxyFacade creates a new ElrondProxyFacade instance
Expand All @@ -61,6 +62,7 @@ func NewElrondProxyFacade(
pubKeyConverter core.PubkeyConverter,
esdtSuppliesProc ESDTSupplyProcessor,
statusProc StatusProcessor,
aboutInfoProc AboutInfoProcessor,
) (*ElrondProxyFacade, error) {
if actionsProc == nil {
return nil, ErrNilActionsProcessor
Expand Down Expand Up @@ -101,6 +103,9 @@ func NewElrondProxyFacade(
if statusProc == nil {
return nil, ErrNilStatusProcessor
}
if aboutInfoProc == nil {
return nil, ErrNilAboutInfoProcessor
}

return &ElrondProxyFacade{
actionsProc: actionsProc,
Expand All @@ -117,6 +122,7 @@ func NewElrondProxyFacade(
pubKeyConverter: pubKeyConverter,
esdtSuppliesProc: esdtSuppliesProc,
statusProc: statusProc,
aboutInfoProc: aboutInfoProc,
}, nil
}

Expand Down Expand Up @@ -469,3 +475,8 @@ func (epf *ElrondProxyFacade) GetGenesisNodesPubKeys() (*data.GenericAPIResponse
func (epf *ElrondProxyFacade) GetGasConfigs() (*data.GenericAPIResponse, error) {
return epf.nodeStatusProc.GetGasConfigs()
}

// GetAboutInfo will return the app info
func (epf *ElrondProxyFacade) GetAboutInfo() (*data.GenericAPIResponse, error) {
return epf.aboutInfoProc.GetAboutInfo(), nil
}
Loading