Documentation ¶
Index ¶
- Constants
- type Joke
- type JokeService
- func (js *JokeService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
- func (js *JokeService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
- func (js *JokeService) Init(core service.FabricServiceCore) error
- func (js *JokeService) OnServerShutdown()
- func (js *JokeService) OnServiceReady() chan bool
- type PingPongService
- func (ps *PingPongService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
- func (ps *PingPongService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
- func (ps *PingPongService) Init(core service.FabricServiceCore) error
- func (ps *PingPongService) OnServerShutdown()
- func (ps *PingPongService) OnServiceReady() chan bool
- type SimpleStreamService
- func (sss *SimpleStreamService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
- func (sss *SimpleStreamService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
- func (sss *SimpleStreamService) Init(core service.FabricServiceCore) error
- func (sss *SimpleStreamService) OnServerShutdown()
- func (sss *SimpleStreamService) OnServiceReady() chan bool
- type StockTickerService
- type TickerMetadata
- type TickerSnapshotData
Constants ¶
const ( StockTickerServiceChannel = "stock-ticker-service" StockTickerAPI = "https://www.alphavantage.co/query" )
const (
JokeServiceChannel = "joke-service"
)
const (
PingPongServiceChan = "ping-pong-service"
)
const (
SimpleStreamServiceChannel = "simple-stream"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JokeService ¶
type JokeService struct {
// contains filtered or unexported fields
}
JokeService will return a terrible joke, on demand.
func NewJokeService ¶
func NewJokeService() *JokeService
NewJokeService will return an instance of JokeService.
func (*JokeService) GetRESTBridgeConfig ¶
func (js *JokeService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
GetRESTBridgeConfig returns a config for a REST endpoint for Jokes
func (*JokeService) HandleServiceRequest ¶
func (js *JokeService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
HandleServiceRequest will listen for incoming requests with the command 'get-joke' and will then return a terrible Joke back to the requesting component.
func (*JokeService) Init ¶
func (js *JokeService) Init(core service.FabricServiceCore) error
Init will fire when the service is being registered by the fabric, it passes a reference of the same core Passed through when implementing HandleServiceRequest
func (*JokeService) OnServerShutdown ¶
func (js *JokeService) OnServerShutdown()
OnServerShutdown is not implemented in this service.
func (*JokeService) OnServiceReady ¶
func (js *JokeService) OnServiceReady() chan bool
OnServiceReady has no functionality in this service, so it returns a fired channel.
type PingPongService ¶
type PingPongService struct{}
PingPongService is a very simple service to demonstrate how request-response cycles are handled in Transport & Plank. this service has two requests named "ping-post" and "ping-get", the first accepts the payload and expects it to be of a POJO type (e.g. {"anything": "here"}), whereas the second expects the payload to be a pure string. a request made through the Event Bus API like bus.RequestOnce() will be routed to HandleServiceRequest() which will match the request's Request to the list of available service request types and return the response.
func NewPingPongService ¶
func NewPingPongService() *PingPongService
func (*PingPongService) GetRESTBridgeConfig ¶
func (ps *PingPongService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
GetRESTBridgeConfig returns a list of REST bridge configurations that Plank will use to automatically register REST endpoints that map to the requests for this service. this means you can map any request types defined under HandleServiceRequest with any combination of URI, HTTP verb, path parameter, query parameter and request headers. as the service author you have full control over every aspect of the translation process which basically turns an incoming *http.Request into model.Request. See FabricRequestBuilder below to see it in action.
func (*PingPongService) HandleServiceRequest ¶
func (ps *PingPongService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
HandleServiceRequest routes the incoming request and based on the Request property of request, it invokes the appropriate handler logic defined and separated by a switch statement like the one shown below.
func (*PingPongService) Init ¶
func (ps *PingPongService) Init(core service.FabricServiceCore) error
Init will fire when the service is being registered by the fabric, it passes a reference of the same core Passed through when implementing HandleServiceRequest
func (*PingPongService) OnServerShutdown ¶
func (ps *PingPongService) OnServerShutdown()
OnServerShutdown is the opposite of OnServiceReady. it is called when the server enters graceful shutdown where all the running services need to complete before the server could shut down finally. this method does not need to return anything because the main server thread is going to shut down soon, but if there's any important teardown or cleanup that needs to be done, this is the right place to perform that.
func (*PingPongService) OnServiceReady ¶
func (ps *PingPongService) OnServiceReady() chan bool
OnServiceReady contains logic that handles the service initialization that needs to be carried out before it is ready to accept user requests. Plank monitors and waits for service initialization to complete by trying to receive a boolean payload from a channel of boolean type. as a service developer you need to perform any and every init logic here and return a channel that would receive a payload once your service truly becomes ready to accept requests.
type SimpleStreamService ¶
type SimpleStreamService struct {
// contains filtered or unexported fields
}
SimpleStreamService broadcasts a simple random word as a string every 1s on channel simple-stream It does not accept any commands and is not listening for them, so there is no point in trying to talk to it. It will only broadcast, you can't stop it and it won't be stopped :)
func NewSimpleStreamService ¶
func NewSimpleStreamService() *SimpleStreamService
NewSimpleStreamService will return a new instance of the SimpleStreamService.
func (*SimpleStreamService) GetRESTBridgeConfig ¶
func (sss *SimpleStreamService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
these lifecycle hooks are not used in this service, bowever we need them to fulfill our contract to enable the OnServiceReady lifecycle method
func (*SimpleStreamService) HandleServiceRequest ¶
func (sss *SimpleStreamService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
func (*SimpleStreamService) Init ¶
func (sss *SimpleStreamService) Init(core service.FabricServiceCore) error
Init will fire when the service is being registered by the fabric, it passes a reference of the same core Passed through when implementing HandleServiceRequest
func (*SimpleStreamService) OnServerShutdown ¶
func (sss *SimpleStreamService) OnServerShutdown()
OnServerShutdown will stop the cronjob firing.
func (*SimpleStreamService) OnServiceReady ¶
func (sss *SimpleStreamService) OnServiceReady() chan bool
OnServiceReady will fetch a list of random words, then set up a timer to broadcast every 500ms, forever.
type StockTickerService ¶
type StockTickerService struct {
// contains filtered or unexported fields
}
StockTickerService is a more complex real life example where its job is to subscribe clients to price updates for a stock symbol. the service accepts a JSON-formatted request from the client that must be formatted like this: {"symbol": "<TICKER_SYMBOL>"}.
once the service receives the request, it will schedule a job to query the stock price API for the provided symbol, retrieve the data and pipe it back to the client every thirty seconds. upon the connected client leaving, the service will remove from its cache the timer.
func NewStockTickerService ¶
func NewStockTickerService() *StockTickerService
NewStockTickerService returns a new instance of StockTickerService
func (*StockTickerService) GetRESTBridgeConfig ¶
func (ps *StockTickerService) GetRESTBridgeConfig() []*service.RESTBridgeConfig
GetRESTBridgeConfig returns a config for a REST endpoint that performs the same action as the STOMP variant except that there will be only one response instead of every 30 seconds.
func (*StockTickerService) HandleServiceRequest ¶
func (ps *StockTickerService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore)
HandleServiceRequest accepts incoming requests and schedules a job to fetch stock price from a third party API and return the results back to the user.
func (*StockTickerService) OnServerShutdown ¶
func (ps *StockTickerService) OnServerShutdown()
OnServerShutdown removes the running tickers
func (*StockTickerService) OnServiceReady ¶
func (ps *StockTickerService) OnServiceReady() chan bool
OnServiceReady sets up a listener to monitor the client STOMP sessions disconnecting from their sessions so that it can stop the running ticker and destroy it from the map structure for individual disconnected clients. this will prevent the service from making unnecessary HTTP calls for the clients even after they are gone and also the memory consumed from ever growing with each connection.
type TickerMetadata ¶
type TickerSnapshotData ¶
type TickerSnapshotData struct { MetaData *TickerMetadata `json:"Meta Data"` TimeSeries map[string]map[string]interface{} `json:"Time Series (1min)"` Note string `json:"Note"` }
TickerSnapshotData and TickerMetadata ares the data structures for this demo service