Skip to content

Commit

Permalink
refactor: improvements on rest v2 server (#22292)
Browse files Browse the repository at this point in the history
(cherry picked from commit 20dd65b)

# Conflicts:
#	server/v2/api/rest/server.go
  • Loading branch information
hieuvubk authored and mergify[bot] committed Oct 17, 2024
1 parent 9e0831b commit 6f11c14
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scripts/init-simapp-v2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 96 additions & 0 deletions server/v2/api/rest/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package rest

import (
"context"
"errors"
"fmt"
"net/http"

"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"

Check failure on line 11 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / split-test-files

no required module provides package cosmossdk.io/server/v2; to add it:

Check failure on line 11 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2; to add it:

Check failure on line 11 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

no required module provides package cosmossdk.io/server/v2; to add it:

Check failure on line 11 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

could not import cosmossdk.io/server/v2 (invalid package name: "")
)

const (
ServerName = "rest"
)

type Server[T transaction.Tx] struct {
logger log.Logger
router *http.ServeMux

httpServer *http.Server
config *Config

Check failure on line 23 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: Config

Check failure on line 23 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: Config (typecheck)
cfgOptions []CfgOption

Check failure on line 24 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: CfgOption

Check failure on line 24 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: CfgOption (typecheck)
}

func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] {

Check failure on line 27 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: CfgOption

Check failure on line 27 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: CfgOption (typecheck)
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)

Check failure on line 40 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: Config

Check failure on line 40 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: Config (typecheck)
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()))

Check failure on line 48 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: NewDefaultHandler

Check failure on line 48 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: NewDefaultHandler (typecheck)
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()

Check failure on line 86 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: DefaultConfig

Check failure on line 86 in server/v2/api/rest/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: DefaultConfig (typecheck)

for _, opt := range s.cfgOptions {
opt(cfg)
}

return cfg
}

return s.config
}
6 changes: 6 additions & 0 deletions tools/confix/data/v2-app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 6f11c14

Please sign in to comment.