Skip to content

Commit 2339b1f

Browse files
committed
Working on exposing a more configurable API.
1 parent 9f3b876 commit 2339b1f

13 files changed

+2600
-2397
lines changed

bus/fabric_endpoint.go

Lines changed: 239 additions & 231 deletions
Large diffs are not rendered by default.

plank/pkg/server/core_models.go

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,97 @@
44
package server
55

66
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"
1313

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"
2222
)
2323

2424
// PlatformServerConfig holds all the core configuration needed for the functionality of Plank
2525
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.
3839
}
3940

4041
// TLSCertConfig wraps around key information for TLS configuration
4142
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)
4546
}
4647

4748
// FabricBrokerConfig defines the endpoint for WebSocket as well as detailed endpoint configuration
4849
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
5354
}
5455

5556
// PlatformServer exposes public API methods that control the behavior of the Plank instance.
5657
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
6869
}
6970

7071
// platformServer is the main struct that holds all components together including servers, various managers etc.
7172
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
8788
}
8889

8990
// MessageBridge is a conduit used for returning service responses as HTTP responses
9091
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
9394
}
9495

9596
// ServerAvailability contains boolean fields to indicate what components of the system are available or not
9697
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
99100
}

plank/pkg/server/initialize.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ func (ps *platformServer) configureFabric() {
155155
ps.HttpServer,
156156
ps.router,
157157
ps.serverConfig.FabricConfig.FabricEndpoint,
158-
nil, ps.serverConfig.Logger, ps.serverConfig.Debug) // TODO: consider tightening access by allowing configuring allowedOrigins
158+
nil, ps.serverConfig.Logger, ps.serverConfig.Debug,
159+
ps.serverConfig.SocketCreationFunc) // TODO: consider tightening access by allowing configuring allowedOrigins
159160
}
160161

161162
// if creation of listener fails, crash and burn

0 commit comments

Comments
 (0)