-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
(cherry picked from commit 20dd65b) # Conflicts: # server/v2/api/rest/server.go
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / split-test-files
Check failure on line 11 in server/v2/api/rest/server.go GitHub Actions / dependency-review
Check failure on line 11 in server/v2/api/rest/server.go GitHub Actions / dependency-review
|
||
) | ||
|
||
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 GitHub Actions / dependency-review
|
||
cfgOptions []CfgOption | ||
Check failure on line 24 in server/v2/api/rest/server.go GitHub Actions / dependency-review
|
||
} | ||
|
||
func New[T transaction.Tx](cfgOptions ...CfgOption) *Server[T] { | ||
Check failure on line 27 in server/v2/api/rest/server.go GitHub Actions / dependency-review
|
||
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 GitHub Actions / dependency-review
|
||
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 GitHub Actions / dependency-review
|
||
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 GitHub Actions / dependency-review
|
||
|
||
for _, opt := range s.cfgOptions { | ||
opt(cfg) | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
return s.config | ||
} |