github.com/vmware/transport-go@v1.3.4/plank/pkg/server/core_models.go (about)

     1  // Copyright 2019-2021 VMware, Inc.
     2  // SPDX-License-Identifier: BSD-2-Clause
     3  
     4  package server
     5  
     6  import (
     7  	"crypto/tls"
     8  	"github.com/gorilla/mux"
     9  	"github.com/vmware/transport-go/bus"
    10  	"github.com/vmware/transport-go/model"
    11  	"github.com/vmware/transport-go/plank/pkg/middleware"
    12  	"github.com/vmware/transport-go/plank/utils"
    13  	"github.com/vmware/transport-go/service"
    14  	"github.com/vmware/transport-go/stompserver"
    15  	"io"
    16  	"net/http"
    17  	"os"
    18  	"sync"
    19  	"time"
    20  )
    21  
    22  // PlatformServerConfig holds all the core configuration needed for the functionality of Plank
    23  type PlatformServerConfig struct {
    24  	RootDir           string              `json:"root_dir"`                       // root directory the server should base itself on
    25  	StaticDir         []string            `json:"static_dir"`                     // static content folders that HTTP server should serve
    26  	SpaConfig         *SpaConfig          `json:"spa_config"`                     // single page application configuration
    27  	Host              string              `json:"host"`                           // hostname for the server
    28  	Port              int                 `json:"port"`                           // port for the server
    29  	LogConfig         *utils.LogConfig    `json:"log_config"`                     // log configuration (plank, Http access and error logs)
    30  	FabricConfig      *FabricBrokerConfig `json:"fabric_config"`                  // Fabric (websocket) configuration
    31  	TLSCertConfig     *TLSCertConfig      `json:"tls_config"`                     // TLS certificate configuration
    32  	EnablePrometheus  bool                `json:"enable_prometheus"`              // whether to enable Prometheus for runtime metrics
    33  	Debug             bool                `json:"debug"`                          // enable debug logging
    34  	NoBanner          bool                `json:"no_banner"`                      // start server without displaying the banner
    35  	ShutdownTimeout   time.Duration       `json:"shutdown_timeout_in_minutes"`    // graceful server shutdown timeout in minutes
    36  	RestBridgeTimeout time.Duration       `json:"rest_bridge_timeout_in_minutes"` // rest bridge timeout in minutes
    37  }
    38  
    39  // TLSCertConfig wraps around key information for TLS configuration
    40  type TLSCertConfig struct {
    41  	CertFile                  string `json:"cert_file"`                   // path to certificate file
    42  	KeyFile                   string `json:"key_file"`                    // path to private key file
    43  	SkipCertificateValidation bool   `json:"skip_certificate_validation"` // whether to skip certificate validation (useful for self-signed cert)
    44  }
    45  
    46  // FabricBrokerConfig defines the endpoint for WebSocket as well as detailed endpoint configuration
    47  type FabricBrokerConfig struct {
    48  	FabricEndpoint string              `json:"fabric_endpoint"` // URI to WebSocket endpoint
    49  	UseTCP         bool                `json:"use_tcp"`         // Use TCP instead of WebSocket
    50  	TCPPort        int                 `json:"tcp_port"`        // TCP port to use if UseTCP is true
    51  	EndpointConfig *bus.EndpointConfig `json:"endpoint_config"` // STOMP configuration
    52  }
    53  
    54  // PlatformServer exposes public API methods that control the behavior of the Plank instance.
    55  type PlatformServer interface {
    56  	StartServer(syschan chan os.Signal)                                         // start server
    57  	StopServer()                                                                // stop server
    58  	RegisterService(svc service.FabricService, svcChannel string) error         // register a new service at given channel
    59  	SetHttpChannelBridge(bridgeConfig *service.RESTBridgeConfig)                // set up a REST bridge for a service
    60  	SetStaticRoute(prefix, fullpath string, middlewareFn ...mux.MiddlewareFunc) // set up a static content route
    61  	SetHttpPathPrefixChannelBridge(bridgeConfig *service.RESTBridgeConfig)      // set up a REST bridge for a path prefix for a service.
    62  	CustomizeTLSConfig(tls *tls.Config) error                                   // used to replace default tls.Config for HTTP server with a custom config
    63  	GetRestBridgeSubRoute(uri, method string) (*mux.Route, error)               // get *mux.Route that maps to the provided uri and method
    64  	GetMiddlewareManager() middleware.MiddlewareManager                         // get middleware manager
    65  
    66  }
    67  
    68  // platformServer is the main struct that holds all components together including servers, various managers etc.
    69  type platformServer struct {
    70  	HttpServer                   *http.Server                      // Http server instance
    71  	SyscallChan                  chan os.Signal                    // syscall channel to receive SIGINT, SIGKILL events
    72  	eventbus                     bus.EventBus                      // event bus pointer
    73  	serverConfig                 *PlatformServerConfig             // server config instance
    74  	middlewareManager            middleware.MiddlewareManager      // middleware maanger instance
    75  	router                       *mux.Router                       // *mux.Router instance
    76  	routerConcurrencyProtection  *int32                            // atomic int32 to protect the main router being concurrently written to
    77  	out                          io.Writer                         // platform log output pointer
    78  	endpointHandlerMap           map[string]http.HandlerFunc       // internal map to store rest endpoint -handler mappings
    79  	serviceChanToBridgeEndpoints map[string][]string               // internal map to store service channel - endpoint handler key mappings
    80  	fabricConn                   stompserver.RawConnectionListener // WebSocket listener instance
    81  	ServerAvailability           *ServerAvailability               // server availability (not much used other than for internal monitoring for now)
    82  	lock                         sync.Mutex                        // lock
    83  	messageBridgeMap             map[string]*MessageBridge
    84  }
    85  
    86  // MessageBridge is a conduit used for returning service responses as HTTP responses
    87  type MessageBridge struct {
    88  	ServiceListenStream bus.MessageHandler  // message handler returned by bus.ListenStream responsible for relaying back messages as HTTP responses
    89  	payloadChannel      chan *model.Message // internal golang channel used for passing bus responses/errors across goroutines
    90  }
    91  
    92  // ServerAvailability contains boolean fields to indicate what components of the system are available or not
    93  type ServerAvailability struct {
    94  	Http   bool // Http server availability
    95  	Fabric bool // stomp broker availability
    96  }