Skip to content

Commit

Permalink
feat: handle send rest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 4, 2022
1 parent 827afce commit 7ddd14c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
26 changes: 16 additions & 10 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"net/http"
"okp4/cosmos-faucet/rest"
)

// NewStartCommand returns a CLI command to start the REST api allowing to send tokens.
func NewStartCommand() *cobra.Command {
startCmd := &cobra.Command{
Use: "start",
Short: "Start the REST api",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
startCmd := &cobra.Command{
Use: "start",
Short: "Start the REST api",
RunE: func(cmd *cobra.Command, args []string) error {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/send/{address}", rest.NewSendRequestHandlerFn(config)).Methods("GET")

return startCmd
return http.ListenAndServe(":10000", router)
},
}

return startCmd
}

func init() {
rootCmd.AddCommand(NewStartCommand())
rootCmd.AddCommand(NewStartCommand())
}
22 changes: 22 additions & 0 deletions rest/send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"net/http"
"okp4/cosmos-faucet/pkg/send"
"okp4/cosmos-faucet/util"
)

// NewSendRequestHandlerFn returns an HTTP REST handler for make transaction to a given address.
func NewSendRequestHandlerFn(config util.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32Addr := vars["address"]

err := send.SendTx(config, bech32Addr)
if rest.CheckBadRequestError(w, err) {
return
}
}
}

0 comments on commit 7ddd14c

Please sign in to comment.