generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |