cmd/baseserver, */rest: allow baseserver to choose which handlers to use
Make handlers easily configurable to use in cmd/baseserver/main.go. This way client users can trivially change what functionality they'd like. It involves moving ServeCmd out of client/rest to */main.go and lets client/rest become a bazaar for available mux.Router registrars. Updates #200
This commit is contained in:
+53
-1
@@ -1,13 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/tendermint/basecoin/client/commands"
|
||||
rest "github.com/tendermint/basecoin/client/rest"
|
||||
coinrest "github.com/tendermint/basecoin/modules/coin/rest"
|
||||
"github.com/tendermint/tmlibs/cli"
|
||||
)
|
||||
|
||||
@@ -17,12 +22,59 @@ var srvCli = &cobra.Command{
|
||||
Long: `Baseserver presents a nice (not raw hex) interface to the basecoin blockchain structure.`,
|
||||
}
|
||||
|
||||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "Serve the light REST client for tendermint",
|
||||
Long: "Access basecoin via REST",
|
||||
RunE: serve,
|
||||
}
|
||||
|
||||
const (
|
||||
envPortFlag = "port"
|
||||
defaultAlgo = "ed25519"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_ = serveCmd.PersistentFlags().Int(envPortFlag, 8998, "the port to run the server on")
|
||||
}
|
||||
|
||||
func serve(cmd *cobra.Command, args []string) error {
|
||||
router := mux.NewRouter()
|
||||
|
||||
routeRegistrars := []func(*mux.Router) error{
|
||||
// rest.Keys handlers
|
||||
rest.NewDefaultKeysManager(defaultAlgo).RegisterAllCRUD,
|
||||
|
||||
// Coin send handler
|
||||
coinrest.RegisterCoinSend,
|
||||
// Coin query account handler
|
||||
coinrest.RegisterQueryAccount,
|
||||
|
||||
// Basecoin sign transactions handler
|
||||
rest.RegisterSignTx,
|
||||
// Basecoin post transaction handler
|
||||
rest.RegisterPostTx,
|
||||
}
|
||||
|
||||
for _, routeRegistrar := range routeRegistrars {
|
||||
if err := routeRegistrar(router); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
port := viper.GetInt(envPortFlag)
|
||||
addr := fmt.Sprintf(":%d", port)
|
||||
|
||||
log.Printf("Serving on %q", addr)
|
||||
return http.ListenAndServe(addr, router)
|
||||
}
|
||||
|
||||
func main() {
|
||||
commands.AddBasicFlags(srvCli)
|
||||
|
||||
srvCli.AddCommand(
|
||||
commands.InitCmd,
|
||||
rest.ServeCmd,
|
||||
serveCmd,
|
||||
)
|
||||
|
||||
// TODO: Decide whether to use $HOME/.basecli for compatibility
|
||||
|
||||
Reference in New Issue
Block a user