Documentation
¶
Overview ¶
Package service defines Fabric service contracts, lifecycle hooks, and REST bridge support.
Index ¶
- Constants
- type ContextFabricService
- type FabricError
- type FabricInitializableService
- type FabricService
- type FabricServiceCore
- type OnServerShutdownEnabled
- type OnServiceReadyEnabled
- type RESTBridgeConfig
- type RESTBridgeEnabled
- type RequestBuilder
- type RestServiceRequest
- type ServiceLifecycleHookEnabled
- type ServiceLifecycleManager
- type ServiceRegistry
- type SetupRESTBridgeRequest
Constants ¶
const ( // LifecycleManagerChannelName is the internal channel for lifecycle requests. LifecycleManagerChannelName = bus.RANCH_INTERNAL_CHANNEL_PREFIX + "service-lifecycle-manager" // ServiceReadyStore is the store used for service readiness notifications. ServiceReadyStore = "service-ready-notification-store" // ServiceInitStateChange is the store state used for service readiness changes. ServiceInitStateChange = "service-init-state-change" )
const RestServiceChannel = "fabric-rest"
RestServiceChannel is the internal channel used by the REST bridge service.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContextFabricService ¶ added in v0.9.0
type ContextFabricService interface {
HandleServiceRequestContext(ctx context.Context, request *model.Request, core FabricServiceCore)
}
ContextFabricService handles Fabric requests with the dispatch context.
type FabricError ¶
type FabricError struct {
Type string `json:"type,omitempty"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Instance string `json:"instance,omitempty"`
}
FabricError is a RFC7807 standard error properties (https://tools.ietf.org/html/rfc7807)
func GetFabricError ¶
func GetFabricError(message string, code int, detail string) FabricError
GetFabricError will return a structured, standardized Error object that is compliant with RFC7807 standard error properties (https://tools.ietf.org/html/rfc7807)
type FabricInitializableService ¶
type FabricInitializableService interface {
Init(core FabricServiceCore) error
}
FabricInitializableService Optional interface, if implemented by a fabric service, its Init method will be invoked when the service is registered in the ServiceRegistry.
type FabricService ¶
type FabricService interface {
// Handles a single Fabric Request
HandleServiceRequest(request *model.Request, core FabricServiceCore)
}
FabricService Interface containing all APIs which should be implemented by Fabric Services.
func NewRestService ¶ added in v0.9.0
func NewRestService() FabricService
NewRestService creates the internal service that performs outbound HTTP requests.
type FabricServiceCore ¶
type FabricServiceCore interface {
// Bus Returns the EventBus instance.
Bus() bus.EventBus
// SendResponse Uses the "responsePayload" and "request" params to build and send model.Response object
// on the service channel.
SendResponse(request *model.Request, responsePayload any)
// SendResponseAsString Uses the "responsePayload" and "request" params to build and send model.Response object
// on the service channel. The payload is not marshalled to JSON, but sent as a string.
SendResponseAsString(request *model.Request, responsePayload string)
// SendResponseAsStringWithHeaders Uses the "responsePayload" and "request" params to build and send model.Response object
// on the service channel. The payload is not marshalled to JSON, but sent as a string... also.. you know.. headers
SendResponseAsStringWithHeaders(request *model.Request, responsePayload string, headers map[string]any)
// SendResponseWithHeaders is the same as SendResponse, but include headers. Useful for HTTP REST interfaces - these headers will be
// set as HTTP response headers. Great for custom mime-types, binary stuff and more.
SendResponseWithHeaders(request *model.Request, responsePayload any, headers map[string]any)
// SendResponseWithHeadersAndCode is the same as SendResponseWithHeaders, but inclides a custom HTTP status code.
SendResponseWithHeadersAndCode(request *model.Request, responsePayload any, headers map[string]any, code int)
// SendErrorResponse builds an error model.Response object and sends it on the service channel as response to the "request" param.
SendErrorResponse(request *model.Request, responseErrorCode int, responseErrorMessage string)
// SendErrorResponseWithPayload is the same as SendErrorResponse, but adds a payload
SendErrorResponseWithPayload(request *model.Request, responseErrorCode int, responseErrorMessage string,
payload any)
// SendErrorResponseWithHeaders is the same as SendErrorResponse, but adds headers as well.
SendErrorResponseWithHeaders(request *model.Request, responseErrorCode int, responseErrorMessage string,
headers map[string]any)
// SendErrorResponseAsStringWithHeadersAndPayload is the same as SendErrorResponseWithPayload, but adds headers as well.
SendErrorResponseAsStringWithHeadersAndPayload(request *model.Request, responseErrorCode int, responseErrorMessage string,
payload string, headers map[string]any)
// SendErrorResponseWithHeadersAndPayload is the same as SendErrorResponseWithPayload, but adds headers as well.
SendErrorResponseWithHeadersAndPayload(request *model.Request, responseErrorCode int, responseErrorMessage string,
payload any, headers map[string]any)
// HandleUnknownRequest handles unknown/unsupported/un-implemented requests,
HandleUnknownRequest(request *model.Request)
// RestServiceRequest will make a new RestService call.
RestServiceRequest(restRequest *RestServiceRequest,
successHandler model.ResponseHandlerFunction, errorHandler model.ResponseHandlerFunction)
// SetHeaders Set global headers for a given fabric service (each service has its own set of global headers).
// The headers will be applied to all requests made by this instance's RestServiceRequest method.
// Global header values can be overridden per request via the RestServiceRequest.Headers property.
SetHeaders(headers map[string]string)
// GenerateJSONHeaders Automatically ready to go map with json headers.
GenerateJSONHeaders() map[string]string
// SetDefaultJSONHeaders Automatically sets default accept and return content types as 'application/json'
SetDefaultJSONHeaders()
}
FabricServiceCore is the interface providing base functionality to fabric services.
type OnServerShutdownEnabled ¶ added in v0.1.0
type OnServerShutdownEnabled interface {
OnServerShutdown() // teardown logic goes here and will be automatically invoked on graceful server shutdown
}
OnServerShutdownEnabled is implemented by services that need graceful shutdown callbacks.
type OnServiceReadyEnabled ¶ added in v0.1.0
type OnServiceReadyEnabled interface {
OnServiceReady() chan bool // service initialization logic should be implemented here
}
OnServiceReadyEnabled is implemented by services that signal initialization completion.
type RESTBridgeConfig ¶
type RESTBridgeConfig struct {
ServiceChannel string // transport service channel
Uri string // URI to map the transport service to
Method string // HTTP verb to map the transport service request to URI with
AllowHead bool // whether HEAD calls are allowed for this bridge point
AllowOptions bool // whether OPTIONS calls are allowed for this bridge point
FabricRequestBuilder RequestBuilder // function to transform HTTP request into a transport request
}
RESTBridgeConfig maps one HTTP endpoint to a Fabric service channel.
type RESTBridgeEnabled ¶ added in v0.1.0
type RESTBridgeEnabled interface {
GetRESTBridgeConfig() []*RESTBridgeConfig // service-to-REST endpoint mappings go here
}
RESTBridgeEnabled is implemented by services that expose REST bridge configuration.
type RequestBuilder ¶
RequestBuilder converts an HTTP request into a Fabric service request.
type RestServiceRequest ¶
type RestServiceRequest struct {
// The destination URL of the request.
Uri string `json:"uri"`
// HTTP Method to use, e.g. GET, POST, PATCH etc.
Method string `json:"method"`
// The body of the request. String and []byte payloads will be sent as is,
// all other payloads will be serialized as json.
Body any `json:"body"`
// HTTP headers of the request.
Headers map[string]string `json:"headers"`
// Optional type of the response body. If provided the service will try to deserialize
// the response to this type.
// If omitted the response body will be deserialized as map[string]any
// Note that if the response body is not a valid json you should set
// the ResponseType to string or []byte otherwise you might get deserialization error
// or empty result.
ResponseType reflect.Type
// Shouldn't be populated directly, the field is used to deserialize
// com.vmware.bifrost.core.model.RestServiceRequest Java/Typescript requests
ApiClass string `json:"apiClass"`
}
RestServiceRequest describes an outbound HTTP request made by the internal REST service.
type ServiceLifecycleHookEnabled ¶
type ServiceLifecycleHookEnabled interface {
OnServiceReady() chan bool // service initialization logic should be implemented here
OnServerShutdown() // teardown logic goes here and will be automatically invoked on graceful server shutdown
GetRESTBridgeConfig() []*RESTBridgeConfig // service-to-REST endpoint mappings go here
}
ServiceLifecycleHookEnabled is implemented by services with ready, shutdown, and REST bridge hooks.
type ServiceLifecycleManager ¶
type ServiceLifecycleManager interface {
GetOnReadyCapableService(serviceChannelName string) OnServiceReadyEnabled
GetOnServerShutdownService(serviceChannelName string) OnServerShutdownEnabled
GetRESTBridgeEnabledService(serviceChannelName string) RESTBridgeEnabled
OverrideRESTBridgeConfig(serviceChannelName string, config []*RESTBridgeConfig) error
}
ServiceLifecycleManager exposes lifecycle-aware service lookup and REST bridge overrides.
func NewServiceLifecycleManager ¶ added in v0.9.0
func NewServiceLifecycleManager(reg ServiceRegistry) ServiceLifecycleManager
NewServiceLifecycleManager creates a lifecycle manager backed by a service registry.
type ServiceRegistry ¶
type ServiceRegistry interface {
// GetAllServiceChannels returns all active Fabric service channels as a slice of strings
GetAllServiceChannels() []string
// RegisterService registers a new fabric service and associates it with a given EventBus channel.
// Only one fabric service can be associated with a given channel.
// If the fabric service implements the FabricInitializableService interface
// its Init method will be called during the registration process.
RegisterService(service FabricService, serviceChannelName string) error
// UnregisterService unregisters the fabric service associated with the given channel.
UnregisterService(serviceChannelName string) error
// SetGlobalRestServiceBaseHost sets the global base host or host:port to be used by the restService
SetGlobalRestServiceBaseHost(host string)
// GetService returns the FabricService for the channel name given as the parameter
GetService(serviceChannelName string) (FabricService, error)
}
ServiceRegistry is the registry for all local fabric services.
func NewServiceRegistry ¶ added in v0.9.0
func NewServiceRegistry(eventBus bus.EventBus, storeManager store.Manager) ServiceRegistry
NewServiceRegistry creates a service registry using the default logger.
func NewServiceRegistryWithLogger ¶ added in v0.9.0
func NewServiceRegistryWithLogger(eventBus bus.EventBus, storeManager store.Manager, logger *slog.Logger) ServiceRegistry
NewServiceRegistryWithLogger creates a service registry using logger, or slog.Default when logger is nil.
type SetupRESTBridgeRequest ¶
type SetupRESTBridgeRequest struct {
ServiceChannel string
Override bool
Config []*RESTBridgeConfig
}
SetupRESTBridgeRequest asks Plank to create or override REST bridge routes for a service.