From 9e93032727878ecbb1bfdb6c309e6de8d8eff948 Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Mon, 13 May 2024 14:12:40 +0300 Subject: [PATCH] Improve airdrop getting handler, add notice comment --- go.mod | 1 + internal/config/broadcaster.go | 7 +++--- internal/service/handlers/get_airdrop.go | 27 +++++++++++++----------- internal/service/requests/get_airdrop.go | 19 +++++++++++++++++ 4 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 internal/service/requests/get_airdrop.go diff --git a/go.mod b/go.mod index 2f1af37..20830ab 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect github.com/armon/go-metrics v0.4.1 // indirect + github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/speakeasy v0.1.0 // indirect github.com/bits-and-blooms/bitset v1.10.0 // indirect diff --git a/internal/config/broadcaster.go b/internal/config/broadcaster.go index fbf67aa..4d6efce 100644 --- a/internal/config/broadcaster.go +++ b/internal/config/broadcaster.go @@ -73,10 +73,11 @@ func (b *broadcasterer) Broadcaster() Broadcaster { panic(fmt.Errorf("broadcaster: invalid airdrop amount: %w", err)) } - tlsConfig := &tls.Config{ + // this hack is required to dial gRPC, please test it with remote RPC if you change this code + withInsecure := grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ InsecureSkipVerify: true, - } - cosmosRPC, err := grpc.Dial(cfg.CosmosRPC, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithKeepaliveParams(keepalive.ClientParameters{ + })) + cosmosRPC, err := grpc.Dial(cfg.CosmosRPC, withInsecure, grpc.WithKeepaliveParams(keepalive.ClientParameters{ Time: 10 * time.Second, // wait time before ping if no activity Timeout: 20 * time.Second, // ping timeout })) diff --git a/internal/service/handlers/get_airdrop.go b/internal/service/handlers/get_airdrop.go index 7b8bacd..15a3012 100644 --- a/internal/service/handlers/get_airdrop.go +++ b/internal/service/handlers/get_airdrop.go @@ -3,39 +3,42 @@ package handlers import ( "net/http" - "github.com/go-chi/chi" - validation "github.com/go-ozzo/ozzo-validation/v4" data "github.com/rarimo/airdrop-svc/internal/data" + "github.com/rarimo/airdrop-svc/internal/service/requests" "github.com/rarimo/airdrop-svc/resources" "gitlab.com/distributed_lab/ape" "gitlab.com/distributed_lab/ape/problems" ) func GetAirdrop(w http.ResponseWriter, r *http.Request) { - var ( - nullifier = chi.URLParam(r, "nullifier") - err = validation.Errors{"{nullifier}": validation.Validate(nullifier, validation.Required)}.Filter() - ) + nullifier, err := requests.NewGetAirdrop(r) if err != nil { ape.RenderErr(w, problems.BadRequest(err)...) return } - airdrop, err := AirdropsQ(r). + airdrops, err := AirdropsQ(r). FilterByNullifier(nullifier). - FilterByStatus(data.TxStatusCompleted). - Get() + Select() if err != nil { - Log(r).WithError(err).Error("Failed to get airdrop by ID") + Log(r).WithError(err).Error("Failed to select airdrops by nullifier") ape.RenderErr(w, problems.InternalError()) return } - if airdrop == nil { + if len(airdrops) == 0 { ape.RenderErr(w, problems.NotFound()) return } - ape.Render(w, toAirdropResponse(*airdrop)) + airdrop := airdrops[0] + for _, a := range airdrops[1:] { + if a.Status == data.TxStatusCompleted { + airdrop = a + break + } + } + + ape.Render(w, toAirdropResponse(airdrop)) } func toAirdropResponse(tx data.Airdrop) resources.AirdropResponse { diff --git a/internal/service/requests/get_airdrop.go b/internal/service/requests/get_airdrop.go new file mode 100644 index 0000000..3626415 --- /dev/null +++ b/internal/service/requests/get_airdrop.go @@ -0,0 +1,19 @@ +package requests + +import ( + "net/http" + + "github.com/go-chi/chi" + val "github.com/go-ozzo/ozzo-validation/v4" + "github.com/go-ozzo/ozzo-validation/v4/is" +) + +func NewGetAirdrop(r *http.Request) (nullifier string, err error) { + nullifier = chi.URLParam(r, "nullifier") + + err = val.Errors{ + "{nullifier}": val.Validate(nullifier, val.Required, is.Digit), + }.Filter() + + return +}