Skip to content

Commit

Permalink
added routes and handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ajansari95 committed Apr 12, 2023
1 parent 0dea9c2 commit 2c1da55
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -59,6 +60,16 @@ func (s *Service) ConfigureRoutes() {

return ctx.JSONBlob(http.StatusOK, data.([]byte))
})

s.Echo.GET("/apr", func(ctx echo.Context) error {
key := "apr"

data, found := s.Cache.Get(key)
if !found {
return s.getAPR(ctx, key)
}
return ctx.JSONBlob(http.StatusOK, data.([]byte))
})
}

func (s *Service) getValidatorList(ctx echo.Context, key string, chainId string) error {
Expand Down Expand Up @@ -225,3 +236,30 @@ func (s *Service) getZones(ctx echo.Context, key string) error {

return ctx.JSONBlob(http.StatusOK, respdata)
}

func (s *Service) getAPR(ctx echo.Context, key string) error {
s.Echo.Logger.Infof("getAPR")
baseurl := "https://chains.cosmos.directory/"

chains := s.Config.Chains
aprResp := APRResponse{}
for _, chain := range chains {
chainAPR, err := getAPRquery(baseurl, chain)
if err != nil {
s.Echo.Logger.Errorf("getAPR: %v - %v", ErrUnableToGetAPR, err)
return ErrUnableToGetAPR
}

aprResp.Chains = append(aprResp.Chains, chainAPR)
}

respdata, err := json.Marshal(aprResp)
if err != nil {
s.Echo.Logger.Errorf("getAPR: %v - %v", ErrMarshalResponse, err)
return ErrMarshalResponse
}

s.Cache.SetWithTTL(key, respdata, 1, 15*time.Minute)

return ctx.JSONBlob(http.StatusOK, respdata)
}

0 comments on commit 2c1da55

Please sign in to comment.