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