From 6f11c14d8921e270e43bfd2595d31450c33e265e Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:03:57 +0700 Subject: [PATCH] refactor: improvements on rest v2 server (#22292) (cherry picked from commit 20dd65b057e13948a9c8ccde7ff9d2224210190b) # Conflicts: # server/v2/api/rest/server.go --- scripts/init-simapp-v2.sh | 2 +- server/v2/api/rest/server.go | 96 +++++++++++++++++++++++++++++++++++ tools/confix/data/v2-app.toml | 6 +++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 server/v2/api/rest/server.go diff --git a/scripts/init-simapp-v2.sh b/scripts/init-simapp-v2.sh index ca25b4f706c0..7410b2174746 100755 --- a/scripts/init-simapp-v2.sh +++ b/scripts/init-simapp-v2.sh @@ -9,7 +9,7 @@ if [ -d "$SIMD_HOME" ]; then rm -rv $SIMD_HOME; fi $SIMD_BIN config set client chain-id simapp-v2-chain $SIMD_BIN config set client keyring-backend test $SIMD_BIN config set client keyring-default-keyname alice -$SIMD_BIN config set app api.enable true +$SIMD_BIN config set app rest.enable true $SIMD_BIN keys add alice --indiscreet $SIMD_BIN keys add bob --indiscreet $SIMD_BIN init simapp-v2-node --chain-id simapp-v2-chain diff --git a/server/v2/api/rest/server.go b/server/v2/api/rest/server.go new file mode 100644 index 000000000000..993501c710eb --- /dev/null +++ b/server/v2/api/rest/server.go @@ -0,0 +1,96 @@ +package rest + +import ( + "context" + "errors" + "fmt" + "net/http" + + "cosmossdk.io/core/transaction" + "cosmossdk.io/log" + serverv2 "cosmossdk.io/server/v2" +) + +const ( + ServerName = "rest" +) + +type Server[T transaction.Tx] struct { + logger log.Logger + router *http.ServeMux + + httpServer *http.Server + config *Config + cfgOptions []CfgOption +} + +func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] { + return &Server[T]{ + cfgOptions: cfgOptions, + } +} + +func (s *Server[T]) Name() string { + return ServerName +} + +func (s *Server[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logger log.Logger) error { + s.logger = logger.With(log.ModuleKey, s.Name()) + + serverCfg := s.Config().(*Config) + if len(cfg) > 0 { + if err := serverv2.UnmarshalSubConfig(cfg, s.Name(), &serverCfg); err != nil { + return fmt.Errorf("failed to unmarshal config: %w", err) + } + } + + s.router = http.NewServeMux() + s.router.Handle("/", NewDefaultHandler(appI.GetAppManager())) + s.config = serverCfg + + return nil +} + +func (s *Server[T]) Start(ctx context.Context) error { + if !s.config.Enable { + s.logger.Info(fmt.Sprintf("%s server is disabled via config", s.Name())) + return nil + } + + s.httpServer = &http.Server{ + Addr: s.config.Address, + Handler: s.router, + } + + s.logger.Info("starting HTTP server", "address", s.config.Address) + if err := s.httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + s.logger.Error("failed to start HTTP server", "error", err) + return err + } + + return nil +} + +func (s *Server[T]) Stop(ctx context.Context) error { + if !s.config.Enable { + return nil + } + + s.logger.Info("stopping HTTP server") + + return s.httpServer.Shutdown(ctx) +} + +func (s *Server[T]) Config() any { + if s.config == nil || s.config.Address == "" { + cfg := DefaultConfig() + + for _, opt := range s.cfgOptions { + opt(cfg) + } + + return cfg + } + + return s.config +} diff --git a/tools/confix/data/v2-app.toml b/tools/confix/data/v2-app.toml index e2aa8a6b066b..6ef684d43afa 100644 --- a/tools/confix/data/v2-app.toml +++ b/tools/confix/data/v2-app.toml @@ -33,6 +33,12 @@ max-recv-msg-size = 10485760 # The default value is math.MaxInt32. max-send-msg-size = 2147483647 +[rest] +# Enable defines if the REST server should be enabled. +enable = true +# Address defines the REST server address to bind to. +address = 'localhost:8080' + [server] # minimum-gas-prices defines the price which a validator is willing to accept for processing a transaction. A transaction's fees must meet the minimum of any denomination specified in this config (e.g. 0.25token1;0.0001token2). minimum-gas-prices = '0stake'