2020-06-15 10:39:09 -07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-06-16 08:11:02 -07:00
|
|
|
"fmt"
|
2020-06-15 10:39:09 -07:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-06-16 08:11:02 -07:00
|
|
|
"strings"
|
2020-06-15 10:39:09 -07:00
|
|
|
"time"
|
|
|
|
|
2020-08-25 08:44:13 -07:00
|
|
|
"github.com/gogo/gateway"
|
2020-06-15 10:39:09 -07:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
2020-08-25 08:44:13 -07:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2020-06-15 10:39:09 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmrpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-07-06 03:04:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
2020-06-15 10:39:09 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/server/config"
|
2020-06-16 08:11:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
2020-08-25 08:44:13 -07:00
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
2020-06-15 10:39:09 -07:00
|
|
|
|
|
|
|
// unnamed import of statik for swagger UI support
|
|
|
|
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server defines the server's API interface.
|
|
|
|
type Server struct {
|
2020-12-04 07:06:50 -08:00
|
|
|
Router *mux.Router
|
|
|
|
GRPCGatewayRouter *runtime.ServeMux
|
|
|
|
ClientCtx client.Context
|
2020-06-15 10:39:09 -07:00
|
|
|
|
|
|
|
logger log.Logger
|
2020-06-16 08:11:02 -07:00
|
|
|
metrics *telemetry.Metrics
|
2020-06-15 10:39:09 -07:00
|
|
|
listener net.Listener
|
|
|
|
}
|
|
|
|
|
2020-08-25 08:44:13 -07:00
|
|
|
// CustomGRPCHeaderMatcher for mapping request headers to
|
|
|
|
// GRPC metadata.
|
|
|
|
// HTTP headers that start with 'Grpc-Metadata-' are automatically mapped to
|
|
|
|
// gRPC metadata after removing prefix 'Grpc-Metadata-'. We can use this
|
|
|
|
// CustomGRPCHeaderMatcher if headers don't start with `Grpc-Metadata-`
|
|
|
|
func CustomGRPCHeaderMatcher(key string) (string, bool) {
|
|
|
|
switch strings.ToLower(key) {
|
|
|
|
case grpctypes.GRPCBlockHeightHeader:
|
|
|
|
return grpctypes.GRPCBlockHeightHeader, true
|
|
|
|
default:
|
|
|
|
return runtime.DefaultHeaderMatcher(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 09:30:49 -07:00
|
|
|
func New(clientCtx client.Context, logger log.Logger) *Server {
|
2020-08-25 08:44:13 -07:00
|
|
|
// The default JSON marshaller used by the gRPC-Gateway is unable to marshal non-nullable non-scalar fields.
|
|
|
|
// Using the gogo/gateway package with the gRPC-Gateway WithMarshaler option fixes the scalar field marshalling issue.
|
|
|
|
marshalerOption := &gateway.JSONPb{
|
|
|
|
EmitDefaults: true,
|
|
|
|
Indent: " ",
|
|
|
|
OrigName: true,
|
2020-10-23 06:49:18 -07:00
|
|
|
AnyResolver: clientCtx.InterfaceRegistry,
|
2020-08-25 08:44:13 -07:00
|
|
|
}
|
|
|
|
|
2020-06-15 10:39:09 -07:00
|
|
|
return &Server{
|
|
|
|
Router: mux.NewRouter(),
|
|
|
|
ClientCtx: clientCtx,
|
2020-06-26 09:30:49 -07:00
|
|
|
logger: logger,
|
2020-12-04 07:06:50 -08:00
|
|
|
GRPCGatewayRouter: runtime.NewServeMux(
|
2020-08-25 08:44:13 -07:00
|
|
|
// Custom marshaler option is required for gogo proto
|
|
|
|
runtime.WithMarshalerOption(runtime.MIMEWildcard, marshalerOption),
|
|
|
|
|
|
|
|
// This is necessary to get error details properly
|
|
|
|
// marshalled in unary requests.
|
|
|
|
runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler),
|
|
|
|
|
|
|
|
// Custom header matcher for mapping request headers to
|
|
|
|
// GRPC metadata
|
|
|
|
runtime.WithIncomingHeaderMatcher(CustomGRPCHeaderMatcher),
|
|
|
|
),
|
2020-06-15 10:39:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the API server. Internally, the API server leverages Tendermint's
|
|
|
|
// JSON RPC server. Configuration options are provided via config.APIConfig
|
|
|
|
// and are delegated to the Tendermint JSON RPC server. The process is
|
|
|
|
// non-blocking, so an external signal handler must be used.
|
2020-06-16 08:11:02 -07:00
|
|
|
func (s *Server) Start(cfg config.Config) error {
|
|
|
|
if cfg.Telemetry.Enabled {
|
|
|
|
m, err := telemetry.New(cfg.Telemetry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.metrics = m
|
|
|
|
s.registerMetrics()
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:39:09 -07:00
|
|
|
tmCfg := tmrpcserver.DefaultConfig()
|
2020-06-16 08:11:02 -07:00
|
|
|
tmCfg.MaxOpenConnections = int(cfg.API.MaxOpenConnections)
|
|
|
|
tmCfg.ReadTimeout = time.Duration(cfg.API.RPCReadTimeout) * time.Second
|
|
|
|
tmCfg.WriteTimeout = time.Duration(cfg.API.RPCWriteTimeout) * time.Second
|
|
|
|
tmCfg.MaxBodyBytes = int64(cfg.API.RPCMaxBodyBytes)
|
2020-06-15 10:39:09 -07:00
|
|
|
|
2021-11-16 11:24:38 -08:00
|
|
|
listener, err := tmrpcserver.Listen(cfg.API.Address, tmCfg.MaxOpenConnections)
|
2020-06-15 10:39:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-28 04:39:49 -07:00
|
|
|
s.registerGRPCGatewayRoutes()
|
2020-09-18 17:34:56 -07:00
|
|
|
|
2020-06-15 10:39:09 -07:00
|
|
|
s.listener = listener
|
|
|
|
var h http.Handler = s.Router
|
|
|
|
|
2020-06-16 08:11:02 -07:00
|
|
|
if cfg.API.EnableUnsafeCORS {
|
2020-07-27 06:58:07 -07:00
|
|
|
allowAllCORS := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))
|
|
|
|
return tmrpcserver.Serve(s.listener, allowAllCORS(h), s.logger, tmCfg)
|
2020-06-15 10:39:09 -07:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:25:10 -07:00
|
|
|
s.logger.Info("starting API server...")
|
2020-06-15 10:39:09 -07:00
|
|
|
return tmrpcserver.Serve(s.listener, s.Router, s.logger, tmCfg)
|
|
|
|
}
|
|
|
|
|
2020-06-16 10:31:35 -07:00
|
|
|
// Close closes the API server.
|
|
|
|
func (s *Server) Close() error {
|
|
|
|
return s.listener.Close()
|
|
|
|
}
|
|
|
|
|
2020-10-28 04:39:49 -07:00
|
|
|
func (s *Server) registerGRPCGatewayRoutes() {
|
2020-12-04 07:06:50 -08:00
|
|
|
s.Router.PathPrefix("/").Handler(s.GRPCGatewayRouter)
|
2020-06-15 10:39:09 -07:00
|
|
|
}
|
2020-06-16 08:11:02 -07:00
|
|
|
|
|
|
|
func (s *Server) registerMetrics() {
|
|
|
|
metricsHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
format := strings.TrimSpace(r.FormValue("format"))
|
|
|
|
|
|
|
|
gr, err := s.metrics.Gather(format)
|
|
|
|
if err != nil {
|
2021-07-06 03:04:54 -07:00
|
|
|
writeErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to gather metrics: %s", err))
|
2020-06-16 08:11:02 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", gr.ContentType)
|
|
|
|
_, _ = w.Write(gr.Metrics)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Router.HandleFunc("/metrics", metricsHandler).Methods("GET")
|
|
|
|
}
|
2021-07-06 03:04:54 -07:00
|
|
|
|
|
|
|
// errorResponse defines the attributes of a JSON error response.
|
|
|
|
type errorResponse struct {
|
|
|
|
Code int `json:"code,omitempty"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// newErrorResponse creates a new errorResponse instance.
|
|
|
|
func newErrorResponse(code int, err string) errorResponse {
|
|
|
|
return errorResponse{Code: code, Error: err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeErrorResponse prepares and writes a HTTP error
|
|
|
|
// given a status code and an error message.
|
|
|
|
func writeErrorResponse(w http.ResponseWriter, status int, err string) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
_, _ = w.Write(legacy.Cdc.MustMarshalJSON(newErrorResponse(0, err)))
|
|
|
|
}
|