2018-02-23 02:25:25 -08:00
|
|
|
package lcd
|
|
|
|
|
|
|
|
import (
|
2018-11-08 16:41:57 -08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-05-28 06:46:26 -07:00
|
|
|
"net/http"
|
2018-11-08 16:41:57 -08:00
|
|
|
"os"
|
2019-05-07 10:04:48 -07:00
|
|
|
"time"
|
2018-11-08 16:41:57 -08:00
|
|
|
|
2018-07-09 11:52:24 -07:00
|
|
|
"github.com/gorilla/mux"
|
2019-05-28 06:46:26 -07:00
|
|
|
"github.com/rakyll/statik/fs"
|
2018-07-09 11:52:24 -07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-11-27 00:14:22 -08:00
|
|
|
rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
|
2018-11-19 09:02:34 -08:00
|
|
|
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
keybase "github.com/cosmos/cosmos-sdk/crypto/keys"
|
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
2019-05-28 06:46:26 -07:00
|
|
|
|
|
|
|
// unnamed import of statik for swagger UI support
|
|
|
|
_ "github.com/cosmos/cosmos-sdk/client/lcd/statik"
|
2018-02-23 02:25:25 -08:00
|
|
|
)
|
|
|
|
|
2018-11-16 14:21:36 -08:00
|
|
|
// RestServer represents the Light Client Rest server
|
|
|
|
type RestServer struct {
|
|
|
|
Mux *mux.Router
|
|
|
|
CliCtx context.CLIContext
|
|
|
|
KeyBase keybase.Keybase
|
2018-02-23 02:25:25 -08:00
|
|
|
|
2019-06-21 08:18:01 -07:00
|
|
|
log log.Logger
|
|
|
|
listener net.Listener
|
2018-11-16 14:21:36 -08:00
|
|
|
}
|
2018-06-11 18:12:37 -07:00
|
|
|
|
2018-11-16 14:21:36 -08:00
|
|
|
// NewRestServer creates a new rest server instance
|
|
|
|
func NewRestServer(cdc *codec.Codec) *RestServer {
|
|
|
|
r := mux.NewRouter()
|
2019-06-19 07:20:27 -07:00
|
|
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
2018-11-16 14:21:36 -08:00
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "rest-server")
|
2018-07-09 11:52:24 -07:00
|
|
|
|
2018-11-16 14:21:36 -08:00
|
|
|
return &RestServer{
|
|
|
|
Mux: r,
|
|
|
|
CliCtx: cliCtx,
|
2019-05-07 10:04:48 -07:00
|
|
|
log: logger,
|
2018-11-16 14:21:36 -08:00
|
|
|
}
|
2018-02-23 02:25:25 -08:00
|
|
|
}
|
2018-02-28 23:16:54 -08:00
|
|
|
|
2018-11-16 14:21:36 -08:00
|
|
|
// Start starts the rest server
|
2019-05-07 10:04:48 -07:00
|
|
|
func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint) (err error) {
|
2018-11-16 14:21:36 -08:00
|
|
|
server.TrapSignal(func() {
|
|
|
|
err := rs.listener.Close()
|
|
|
|
rs.log.Error("error closing listener", "err", err)
|
|
|
|
})
|
|
|
|
|
2019-05-07 10:04:48 -07:00
|
|
|
cfg := &rpcserver.Config{
|
|
|
|
MaxOpenConnections: maxOpen,
|
|
|
|
ReadTimeout: time.Duration(readTimeout) * time.Second,
|
|
|
|
WriteTimeout: time.Duration(writeTimeout) * time.Second,
|
|
|
|
}
|
2019-03-29 10:13:45 -07:00
|
|
|
|
|
|
|
rs.listener, err = rpcserver.Listen(listenAddr, cfg)
|
2019-01-17 05:06:33 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-29 10:13:45 -07:00
|
|
|
rs.log.Info(
|
|
|
|
fmt.Sprintf(
|
2019-05-18 01:42:24 -07:00
|
|
|
"Starting application REST service (chain-id: %q)...",
|
2019-05-28 01:44:04 -07:00
|
|
|
viper.GetString(flags.FlagChainID),
|
2019-03-29 10:13:45 -07:00
|
|
|
),
|
|
|
|
)
|
2019-01-17 05:06:33 -08:00
|
|
|
|
2019-03-29 10:13:45 -07:00
|
|
|
return rpcserver.StartHTTPServer(rs.listener, rs.Mux, rs.log, cfg)
|
2018-11-16 14:21:36 -08:00
|
|
|
}
|
2018-09-13 05:30:24 -07:00
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// ServeCommand will start the application REST service as a blocking process. It
|
2018-11-27 13:03:53 -08:00
|
|
|
// takes a codec to create a RestServer object and a function to register all
|
|
|
|
// necessary routes.
|
|
|
|
func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.Command {
|
2018-11-16 14:21:36 -08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rest-server",
|
|
|
|
Short: "Start LCD (light-client daemon), a local REST server",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
2018-11-27 13:03:53 -08:00
|
|
|
rs := NewRestServer(cdc)
|
|
|
|
|
|
|
|
registerRoutesFn(rs)
|
2019-05-28 06:46:26 -07:00
|
|
|
rs.registerSwaggerUI()
|
2018-11-27 13:03:53 -08:00
|
|
|
|
2018-11-16 14:21:36 -08:00
|
|
|
// Start the rest server and return error if one exists
|
2019-05-07 10:04:48 -07:00
|
|
|
err = rs.Start(
|
2019-05-28 01:44:04 -07:00
|
|
|
viper.GetString(flags.FlagListenAddr),
|
|
|
|
viper.GetInt(flags.FlagMaxOpenConnections),
|
|
|
|
uint(viper.GetInt(flags.FlagRPCReadTimeout)),
|
|
|
|
uint(viper.GetInt(flags.FlagRPCWriteTimeout)),
|
2019-05-07 10:04:48 -07:00
|
|
|
)
|
2018-11-16 14:21:36 -08:00
|
|
|
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
2018-09-13 05:30:24 -07:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
return flags.RegisterRestServerFlags(cmd)
|
2018-09-13 23:14:16 -07:00
|
|
|
}
|
2019-05-28 06:46:26 -07:00
|
|
|
|
|
|
|
func (rs *RestServer) registerSwaggerUI() {
|
|
|
|
statikFS, err := fs.New()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
staticServer := http.FileServer(statikFS)
|
|
|
|
rs.Mux.PathPrefix("/swagger-ui/").Handler(http.StripPrefix("/swagger-ui/", staticServer))
|
|
|
|
}
|