Skip to content

Commit

Permalink
feat: add some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 4, 2022
1 parent b9fbe59 commit f5fe13f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"okp4/cosmos-faucet/pkg"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -25,6 +27,7 @@ var rootCmd = &cobra.Command{
Short: "A CØSMOS Faucet",
Long: "CØSMOS Faucet",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: cmd.OutOrStdout()})
return ReadPersistentFlags(cmd)
},
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"okp4/cosmos-faucet/pkg/server"

"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -22,14 +23,15 @@ func NewStartCommand() *cobra.Command {
startCmd := &cobra.Command{
Use: "start",
Short: "Start the REST api",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
faucet, err := client.NewFaucet(config)
if err != nil {
return err
log.Fatal().Err(err).Msg("Failed create a new faucet instance")
}

defer func(faucet *client.Faucet) {
_ = faucet.Close()
log.Info().Msg("Server stopped")
}(faucet)

router := mux.NewRouter().StrictSlash(true)
Expand All @@ -38,7 +40,8 @@ func NewStartCommand() *cobra.Command {
HandlerFunc(server.NewSendRequestHandlerFn(context.Background(), faucet)).
Methods("GET")

return http.ListenAndServe(addr, router)
log.Info().Msgf("Server listening at %s", addr)
log.Fatal().Err(http.ListenAndServe(addr, router)).Msg("Server listening stopped")
},
}

Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module okp4/cosmos-faucet
go 1.18

require (
github.com/cosmos/btcutil v1.0.4
github.com/cosmos/cosmos-sdk v0.45.4
github.com/cosmos/go-bip39 v1.0.0
github.com/gorilla/mux v1.8.0
github.com/rs/zerolog v1.23.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.11.0
Expand All @@ -25,6 +25,8 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/confio/ics23/go v0.6.6 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/iavl v0.17.3 // indirect
github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
Expand All @@ -47,7 +49,6 @@ require (
github.com/golang/snappy v0.0.3 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.23.0 h1:UskrK+saS9P9Y789yNNulYKdARjPZuS35B8gJF2x60g=
github.com/rs/zerolog v1.23.0/go.mod h1:6c7hFfxPOy7TacJc4Fcdi24/J0NKYGzjG8FWRI916Qo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
10 changes: 10 additions & 0 deletions pkg/server/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)

// NewSendRequestHandlerFn returns an HTTP REST handler for make transaction to a given address.
Expand All @@ -16,7 +17,16 @@ func NewSendRequestHandlerFn(ctx context.Context, faucet *client.Faucet) http.Ha
vars := mux.Vars(r)
bech32Addr := vars["address"]

log.Info().Str("toAddress", bech32Addr).
Str("fromAddress", faucet.FromAddr.String()).
Msgf("Send %d%s to %s...", faucet.Config.AmountSend, faucet.Config.Denom, bech32Addr)

err := faucet.SendTxMsg(ctx, bech32Addr)

if err != nil {
log.Err(err).Msg("Transaction failed.")
}

rest.CheckBadRequestError(w, err)
}
}

0 comments on commit f5fe13f

Please sign in to comment.