Skip to content

Commit

Permalink
Added /v1/health endpoint and update rpc spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecuesta authored and oten91 committed Dec 1, 2022
1 parent 785379d commit cfe13ac
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/cmd/rpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func Version(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
WriteResponse(w, APIVersion, r.URL.Path, r.Host)
}

func Health(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
res := app.PCA.QueryHealth(APIVersion)
j, er := json.Marshal(res)
if er != nil {
WriteErrorResponse(w, 400, er.Error())
return
}
WriteJSONResponse(w, string(j), r.URL.Path, r.Host)
}

func LocalNodes(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
value := r.URL.Query().Get("authtoken")
if value != app.AuthToken.Value {
Expand Down
1 change: 1 addition & 0 deletions app/cmd/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Routes []Route
func GetRoutes() Routes {
routes := Routes{
Route{Name: "AppVersion", Method: "GET", Path: "/v1", HandlerFunc: Version},
Route{Name: "Health", Method: "GET", Path: "/v1/health", HandlerFunc: Health},
Route{Name: "Challenge", Method: "POST", Path: "/v1/client/challenge", HandlerFunc: Challenge},
Route{Name: "ChallengeCORS", Method: "OPTIONS", Path: "/v1/client/challenge", HandlerFunc: Challenge},
Route{Name: "HandleDispatch", Method: "POST", Path: "/v1/client/dispatch", HandlerFunc: Dispatch},
Expand Down
31 changes: 31 additions & 0 deletions app/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ const (
txHeightQuery = "tx.height=%d"
)

type HealthResponse struct {
Version string `json:"version"`
IsStarting bool `json:"is_starting"`
IsCatchingUp bool `json:"is_catching_up"`
Height int64 `json:"height"`
}

func (app PocketCoreApp) QueryHealth(version string) (res HealthResponse) {
res = HealthResponse{
Version: version,
IsStarting: true,
IsCatchingUp: false,
Height: 0,
}
res.Height = app.LastBlockHeight()
_, err := app.NewContext(res.Height)

if err != nil {
return
}

status, sErr := app.pocketKeeper.TmNode.ConsensusReactorStatus()
if sErr != nil {
return
}

res.IsStarting = false
res.IsCatchingUp = status.IsCatchingUp
return
}

// zero for height = latest
func (app PocketCoreApp) QueryBlock(height *int64) (blockJSON []byte, err error) {
tmClient := app.GetClient()
Expand Down
25 changes: 25 additions & 0 deletions doc/specs/rpc-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ servers:
tags:
- name: version
description: Version of the Pocket API
- name: health
description: Health of the Pocket Node
- name: client
description: Dispatch and relay services
- name: query
Expand All @@ -35,6 +37,18 @@ paths:
schema:
type: string
example: 0.0.1
/health:
get:
tags:
- health
summary: Get current node version, height, starting and running state
responses:
'200':
description: Health status of the node
content:
application/json:
schema:
$ref: '#/components/schemas/Health'
/client/dispatch:
post:
tags:
Expand Down Expand Up @@ -1464,6 +1478,17 @@ components:
properties:
address:
type: string
Health:
type: object
properties:
version:
type: string
height:
type: integer
is_starting:
type: boolean
is_catching_up:
type: boolean
Chain:
type: object
properties:
Expand Down

0 comments on commit cfe13ac

Please sign in to comment.