Documentation ¶
Index ¶
- Constants
- Variables
- type AboutOperations
- func (c *AboutOperations) About(res http.ResponseWriter, req *http.Request)
- func (c *AboutOperations) GetAboutOperation() func(res http.ResponseWriter, req *http.Request)
- func (c *AboutOperations) GetNetworkAddresses() []string
- func (c *AboutOperations) SetReferences(ctx context.Context, references crefer.IReferences)
- type CommandableHttpService
- type CommandableSwaggerDocument
- type HeartbeatOperations
- type HeartbeatRestService
- type HttpEndpoint
- func (c *HttpEndpoint) AddCorsHeader(header string, origin string)
- func (c *HttpEndpoint) Close(ctx context.Context, correlationId string) error
- func (c *HttpEndpoint) Configure(ctx context.Context, config *cconf.ConfigParams)
- func (c *HttpEndpoint) GetCorrelationId(req *http.Request) string
- func (c *HttpEndpoint) IsOpen() bool
- func (c *HttpEndpoint) Open(ctx context.Context, correlationId string) error
- func (c *HttpEndpoint) Register(registration IRegisterable)
- func (c *HttpEndpoint) RegisterInterceptor(route string, ...)
- func (c *HttpEndpoint) RegisterRoute(method string, route string, schema *cvalid.Schema, action http.HandlerFunc)
- func (c *HttpEndpoint) RegisterRouteWithAuth(method string, route string, schema *cvalid.Schema, ...)
- func (c *HttpEndpoint) SetReferences(ctx context.Context, references crefer.IReferences)
- func (c *HttpEndpoint) Unregister(registration IRegisterable)
- type IRegisterable
- type ISchemaBaseWithValueType
- type ISchemaWithProperties
- type ISwaggerService
- type InstrumentTiming
- type RestOperations
- func (c *RestOperations) Configure(ctx context.Context, config *cconf.ConfigParams)
- func (c *RestOperations) DecodeBody(req *http.Request, target any) error
- func (c *RestOperations) GetCorrelationId(req *http.Request) string
- func (c *RestOperations) GetFilterParams(req *http.Request) *cdata.FilterParams
- func (c *RestOperations) GetPagingParams(req *http.Request) *cdata.PagingParams
- func (c *RestOperations) GetParam(req *http.Request, name string) string
- func (c *RestOperations) SendBadRequest(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SendConflict(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SendCreatedResult(res http.ResponseWriter, req *http.Request, result any, err error)
- func (c *RestOperations) SendDeletedResult(res http.ResponseWriter, req *http.Request, result any, err error)
- func (c *RestOperations) SendEmptyResult(res http.ResponseWriter, req *http.Request, err error)
- func (c *RestOperations) SendError(res http.ResponseWriter, req *http.Request, err error)
- func (c *RestOperations) SendInternalError(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SendNotFound(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SendResult(res http.ResponseWriter, req *http.Request, result any, err error)
- func (c *RestOperations) SendServerUnavailable(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SendSessionExpired(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SendUnauthorized(res http.ResponseWriter, req *http.Request, message string)
- func (c *RestOperations) SetReferences(ctx context.Context, references crefer.IReferences)
- type RestService
- func (c *RestService) Close(ctx context.Context, correlationId string) error
- func (c *RestService) Configure(ctx context.Context, config *cconf.ConfigParams)
- func (c *RestService) DecodeBody(req *http.Request, target any) error
- func (c *RestService) GetCorrelationId(req *http.Request) string
- func (c *RestService) GetFilterParams(req *http.Request) *cdata.FilterParams
- func (c *RestService) GetPagingParams(req *http.Request) *cdata.PagingParams
- func (c *RestService) GetParam(req *http.Request, name string) string
- func (c *RestService) Instrument(ctx context.Context, correlationId string, name string) *InstrumentTiming
- func (c *RestService) InstrumentError(ctx context.Context, correlationId string, name string, errIn error, resIn any) (result any, err error)
- func (c *RestService) IsOpen() bool
- func (c *RestService) Open(ctx context.Context, correlationId string) error
- func (c *RestService) Register()
- func (c *RestService) RegisterInterceptor(route string, ...)
- func (c *RestService) RegisterOpenApiSpec(content string)
- func (c *RestService) RegisterOpenApiSpecFromFile(path string)
- func (c *RestService) RegisterRoute(method string, route string, schema *cvalid.Schema, ...)
- func (c *RestService) RegisterRouteWithAuth(method string, route string, schema *cvalid.Schema, ...)
- func (c *RestService) SendCreatedResult(res http.ResponseWriter, req *http.Request, result any, err error)
- func (c *RestService) SendDeletedResult(res http.ResponseWriter, req *http.Request, result any, err error)
- func (c *RestService) SendError(res http.ResponseWriter, req *http.Request, err error)
- func (c *RestService) SendResult(res http.ResponseWriter, req *http.Request, result any, err error)
- func (c *RestService) SetReferences(ctx context.Context, references crefer.IReferences)
- func (c *RestService) UnsetReferences()
- type StatusOperations
- type StatusRestService
Constants ¶
const ( DefaultConnectionTimeout = "60000" DefaultFileMaxSize = 200 * 1024 * 1024 DefaultRequestMaxSize = 1024 * 1024 )
Variables ¶
var HttpRequestDetector = _THttpRequestDetector{}
HttpRequestDetector helper class that retrieves parameters from HTTP requests.
var HttpResponseSender = _THttpResponseSender{}
HttpResponseSender helper class that handles HTTP-based responses.
Functions ¶
This section is empty.
Types ¶
type AboutOperations ¶
type AboutOperations struct { *RestOperations // contains filtered or unexported fields }
func NewAboutOperations ¶ added in v1.0.3
func NewAboutOperations() *AboutOperations
func (*AboutOperations) About ¶
func (c *AboutOperations) About(res http.ResponseWriter, req *http.Request)
func (*AboutOperations) GetAboutOperation ¶
func (c *AboutOperations) GetAboutOperation() func(res http.ResponseWriter, req *http.Request)
func (*AboutOperations) GetNetworkAddresses ¶
func (c *AboutOperations) GetNetworkAddresses() []string
func (*AboutOperations) SetReferences ¶
func (c *AboutOperations) SetReferences(ctx context.Context, references crefer.IReferences)
type CommandableHttpService ¶
type CommandableHttpService struct { *RestService SwaggerAuto bool // contains filtered or unexported fields }
CommandableHttpService abstract service that receives remove calls via HTTP/REST protocol to operations automatically generated for commands defined in ICommandable components. Each command is exposed as POST operation that receives all parameters in body object.
Commandable services require only 3 lines of code to implement a robust external HTTP-based remote interface.
Configuration parameters: - base_route: base route for remote URI - dependencies: - endpoint: override for HTTP Endpoint dependency - controller: override for Controller dependency - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - protocol: connection protocol: http or https - host: host name or IP address - port: port number - uri: resource URI or connection string with all parameters in it References: - *:logger:*:*:1.0 (optional) ILogger components to pass log messages - *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection - *:endpoint:http:*:1.0 (optional) HttpEndpoint reference see clients.CommandableHttpClient see RestService Example: type MyCommandableHttpService struct { *CommandableHttpService } func NewMyCommandableHttpService() *MyCommandableHttpService { c := MyCommandableHttpService{ CommandableHttpService: services.NewCommandableHttpService("dummies"), } c.DependencyResolver.Put(context.Background(), "controller", cref.NewDescriptor("pip-services-dummies", "controller", "default", "*", "*")) return &c } service := NewMyCommandableHttpService(); service.Configure(context.Background(), cconf.NewConfigParamsFromTuples( "connection.protocol", "http", "connection.host", "localhost", "connection.port", 8080, )); service.SetReferences(context.Background(), cref.NewReferencesFromTuples( cref.NewDescriptor("mygroup","controller","default","default","1.0"), controller )); opnErr := service.Open(context.Background(), "123") if opnErr == nil { fmt.Println("The REST service is running on port 8080"); }
func InheritCommandableHttpService ¶
func InheritCommandableHttpService(overrides IRegisterable, baseRoute string) *CommandableHttpService
InheritCommandableHttpService creates a new instance of the service.
Parameters: - overrides references to child class that overrides virtual methods - baseRoute string a service base route. Returns: *CommandableHttpService pointer on new instance CommandableHttpService
func (*CommandableHttpService) Configure ¶
func (c *CommandableHttpService) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure method configures component by passing configuration parameters.
Parameters: - ctx context.Context - config configuration parameters to be set.
func (*CommandableHttpService) Register ¶
func (c *CommandableHttpService) Register()
Register method are registers all service routes in HTTP endpoint.
type CommandableSwaggerDocument ¶
type CommandableSwaggerDocument struct { Commands []ccomands.ICommand Version string BaseRoute string InfoTitle string InfoDescription string InfoVersion string InfoTermsOfService string InfoContactName string InfoContactUrl string InfoContactEmail string InfoLicenseName string InfoLicenseUrl string // contains filtered or unexported fields }
func NewCommandableSwaggerDocument ¶
func NewCommandableSwaggerDocument(baseRoute string, config *cconf.ConfigParams, commands []ccomands.ICommand) *CommandableSwaggerDocument
func (*CommandableSwaggerDocument) GetSpaces ¶
func (c *CommandableSwaggerDocument) GetSpaces(length int) string
func (*CommandableSwaggerDocument) ToString ¶
func (c *CommandableSwaggerDocument) ToString() string
func (*CommandableSwaggerDocument) WriteData ¶
func (c *CommandableSwaggerDocument) WriteData(indent int, data map[string]any)
func (*CommandableSwaggerDocument) WriteName ¶
func (c *CommandableSwaggerDocument) WriteName(indent int, name string)
type HeartbeatOperations ¶
type HeartbeatOperations struct {
*RestOperations
}
func NewHeartbeatOperations ¶
func NewHeartbeatOperations() *HeartbeatOperations
NewHeartbeatOperations creates new instance HeartbeatOperations
Returns: *HeartbeatOperations
func (*HeartbeatOperations) GetHeartbeatOperation ¶
func (c *HeartbeatOperations) GetHeartbeatOperation() func(res http.ResponseWriter, req *http.Request)
GetHeartbeatOperation is a heartbeat method are insert timestamp into HTTP result
func (*HeartbeatOperations) Heartbeat ¶
func (c *HeartbeatOperations) Heartbeat(res http.ResponseWriter, req *http.Request)
Heartbeat method are insert timestamp into HTTP result
type HeartbeatRestService ¶
type HeartbeatRestService struct { *RestService // contains filtered or unexported fields }
HeartbeatRestService service returns heartbeat via HTTP/REST protocol. The service responds on /heartbeat route (can be changed) with a string with the current time in UTC. This service route can be used to health checks by load-balancers and container orchestrators.
Configuration parameters: - baseroute: base route for remote URI (default: "") - route: route to heartbeat operation (default: "heartbeat") - dependencies: - endpoint: override for HTTP Endpoint dependency - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - protocol: connection protocol: http or https - host: host name or IP address - port: port number - uri: resource URI or connection string with all parameters in it References: - *:logger:*:*:1.0 (optional) ILogger components to pass log messages - *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection - *:endpoint:http:*:1.0 (optional) HttpEndpoint reference see RestService see clients.RestClient Example: service := NewHeartbeatService(); service.Configure(context.Background(), cconf.NewConfigParamsFromTuples( "route", "ping", "connection.protocol", "http", "connection.host", "localhost", "connection.port", 8080, )); opnErr := service.Open(context.Background(), "123") if opnErr == nil { fmt.Println("The Heartbeat service is accessible at http://+:8080/ping"); }
func NewHeartbeatRestService ¶
func NewHeartbeatRestService() *HeartbeatRestService
NewHeartbeatRestService creates a new instance of c service.
func (*HeartbeatRestService) Configure ¶
func (c *HeartbeatRestService) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure component by passing configuration parameters.
Parameters: - ctx context.Context - config configuration parameters to be set.
func (*HeartbeatRestService) Register ¶
func (c *HeartbeatRestService) Register()
Register all service routes in HTTP endpoint.
type HttpEndpoint ¶
type HttpEndpoint struct {
// contains filtered or unexported fields
}
HttpEndpoint used for creating HTTP endpoints. An endpoint is a URL, at which a given service can be accessed by a client.
Configuration parameters: Parameters to pass to the configure method for component configuration: - cors_headers - a comma-separated list of allowed CORS headers - cors_origins - a comma-separated list of allowed CORS origins - connection(s) - the connection resolver"s connections: - "connection.discovery_key" - the key to use for connection resolving in a discovery service; - "connection.protocol" - the connection"s protocol; - "connection.host" - the target host; - "connection.port" - the target port; - "connection.uri" - the target URI. - credential - the HTTPS credentials: - "credential.ssl_key_file" - the SSL func (c *HttpEndpoint )key in PEM - "credential.ssl_crt_file" - the SSL certificate in PEM - "credential.ssl_ca_file" - the certificate authorities (root cerfiticates) in PEM References: A logger, counters, and a connection resolver can be referenced by passing the following references to the object"s setReferences method: - logger: "*:logger:*:*:1.0"; - counters: "*:counters:*:*:1.0"; - discovery: "*:discovery:*:*:1.0" (for the connection resolver). Examples: endpoint := NewHttpEndpoint(); endpoint.Configure(context.Background(), config); endpoint.SetReferences(context.Background(), references); ... endpoint.Open(context.Background(), correlationId)
func NewHttpEndpoint ¶
func NewHttpEndpoint() *HttpEndpoint
NewHttpEndpoint creates new HttpEndpoint
func (*HttpEndpoint) AddCorsHeader ¶
func (c *HttpEndpoint) AddCorsHeader(header string, origin string)
AddCorsHeader method adds allowed header, ignore if it already exists must be called before to opening endpoint
func (*HttpEndpoint) Close ¶
func (c *HttpEndpoint) Close(ctx context.Context, correlationId string) error
Close method are closes this endpoint and the REST server (service) that was opened earlier.
Parameters: - ctx context.Context - correlationId string (optional) transaction id to trace execution through call chain. Returns: error an error if one is raised.
func (*HttpEndpoint) Configure ¶
func (c *HttpEndpoint) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure method are configures this HttpEndpoint using the given configuration parameters.
Configuration parameters: - connection(s) - the connection resolver"s connections; - "connection.discovery_key" - the key to use for connection resolving in a discovery service; - "connection.protocol" - the connection"s protocol; - "connection.host" - the target host; - "connection.port" - the target port; - "connection.uri" - the target URI. - "credential.ssl_key_file" - SSL func (c *HttpEndpoint )key in PEM - "credential.ssl_crt_file" - SSL certificate in PEM - "credential.ssl_ca_file" - Certificate authority (root certificate) in PEM Parameters: - ctx context.Context - config configuration parameters, containing a "connection(s)" section.
func (*HttpEndpoint) GetCorrelationId ¶
func (c *HttpEndpoint) GetCorrelationId(req *http.Request) string
GetCorrelationId method returns CorrelationId from request
Parameters: - req *http.Request request Returns: string correlation_id or empty string
func (*HttpEndpoint) IsOpen ¶
func (c *HttpEndpoint) IsOpen() bool
IsOpen method is whether this endpoint is open with an actively listening REST server.
func (*HttpEndpoint) Open ¶
func (c *HttpEndpoint) Open(ctx context.Context, correlationId string) error
Open a connection using the parameters resolved by the referenced connection resolver and creates a REST server (service) using the set options and parameters.
Parameters: - ctx context.Context - correlationId string (optional) transaction id to trace execution through call chain. Returns: error an error if one is raised.
func (*HttpEndpoint) Register ¶
func (c *HttpEndpoint) Register(registration IRegisterable)
Register a registrable object for dynamic endpoint discovery.
Parameters: - registration IRegisterable implements of IRegisterable interface. See IRegisterable
func (*HttpEndpoint) RegisterInterceptor ¶
func (c *HttpEndpoint) RegisterInterceptor(route string, action func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))
RegisterInterceptor method are registers a middleware action for the given route. Parameters:
- route the route to register in this object"s REST server (service).
- action the middleware action to perform at the given route.
func (*HttpEndpoint) RegisterRoute ¶
func (c *HttpEndpoint) RegisterRoute(method string, route string, schema *cvalid.Schema, action http.HandlerFunc)
RegisterRoute method are registers an action in this objects REST server (service) by the given method and route.
Parameters: - method string the HTTP method of the route. - route string the route to register in this object"s REST server (service). - schema *cvalid.Schema the schema to use for parameter validation. - action http.HandlerFunc the action to perform at the given route.
func (*HttpEndpoint) RegisterRouteWithAuth ¶
func (c *HttpEndpoint) RegisterRouteWithAuth(method string, route string, schema *cvalid.Schema, authorize func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc), action http.HandlerFunc)
RegisterRouteWithAuth method are registers an action with authorization in this objects REST server (service) by the given method and route. Parameters:
- method string the HTTP method of the route.
- route string the route to register in this object"s REST server (service).
- schema *cvalid.Schema the schema to use for parameter validation.
- authorize the authorization interceptor
- action the action to perform at the given route.
func (*HttpEndpoint) SetReferences ¶
func (c *HttpEndpoint) SetReferences(ctx context.Context, references crefer.IReferences)
SetReferences method are sets references to this endpoint"s logger, counters, and connection resolver.
References: - logger: "*:logger:*:*:1.0" - counters: "*:counters:*:*:1.0" - discovery: "*:discovery:*:*:1.0" (for the connection resolver) Parameters: - references an IReferences object, containing references to a logger, counters, and a connection resolver.
func (*HttpEndpoint) Unregister ¶
func (c *HttpEndpoint) Unregister(registration IRegisterable)
Unregister a registerable object, so that it is no longer used in dynamic endpoint discovery.
Parameters: - registration IRegisterable the registration to remove. See IRegisterable
type IRegisterable ¶
type IRegisterable interface {
// Register perform required registration steps.
Register()
}
IRegisterable is interface to perform on-demand registrations.
type ISchemaBaseWithValueType ¶ added in v1.0.3
type ISchemaBaseWithValueType interface { cvalid.ISchemaBase ValueType() any }
type ISchemaWithProperties ¶ added in v1.0.3
type ISchemaWithProperties interface { cvalid.ISchema Properties() []*cvalid.PropertySchema }
type ISwaggerService ¶
type ISwaggerService interface { // RegisterOpenApiSpec Perform required Swagger registration steps. RegisterOpenApiSpec(baseRoute string, swaggerRoute string) }
ISwaggerService Interface to perform Swagger registrations.
type InstrumentTiming ¶
type InstrumentTiming struct {
// contains filtered or unexported fields
}
func NewInstrumentTiming ¶
func NewInstrumentTiming(correlationId string, name string, verb string, logger clog.ILogger, counters ccount.ICounters, counterTiming *ccount.CounterTiming, traceTiming *ctrace.TraceTiming) *InstrumentTiming
func (*InstrumentTiming) EndFailure ¶
func (c *InstrumentTiming) EndFailure(ctx context.Context, err error)
func (*InstrumentTiming) EndSuccess ¶
func (c *InstrumentTiming) EndSuccess(ctx context.Context)
type RestOperations ¶
type RestOperations struct { Logger *clog.CompositeLogger Counters *ccount.CompositeCounters DependencyResolver *crefer.DependencyResolver }
RestOperations helper class for REST operations
func NewRestOperations ¶
func NewRestOperations() *RestOperations
NewRestOperations creates new instance of RestOperations
func (*RestOperations) Configure ¶
func (c *RestOperations) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure method are configures this RestOperations using the given configuration parameters.
Parameters: - ctx context.Context - config *cconf.ConfigParams confif parameters
func (*RestOperations) DecodeBody ¶
func (c *RestOperations) DecodeBody(req *http.Request, target any) error
DecodeBody methods helps decode body
Parameters: - req incoming request - target pointer on target variable for decode
Returns: error
func (*RestOperations) GetCorrelationId ¶
func (c *RestOperations) GetCorrelationId(req *http.Request) string
GetCorrelationId method returns CorrelationId from request
Parameters: - req *http.Request request Returns: string correlation_id or empty string
func (*RestOperations) GetFilterParams ¶
func (c *RestOperations) GetFilterParams(req *http.Request) *cdata.FilterParams
GetFilterParams method reruns filter params object from request
Parameters: - req *http.Request request Returns: *cdata.FilterParams filter params object
func (*RestOperations) GetPagingParams ¶
func (c *RestOperations) GetPagingParams(req *http.Request) *cdata.PagingParams
GetPagingParams method reruns paging params object from request
Parameters: - req *http.Request request Returns: *cdata.PagingParams pagings params object
func (*RestOperations) GetParam ¶
func (c *RestOperations) GetParam(req *http.Request, name string) string
GetParam methods helps get all params from query
Parameters: - req incoming request - name parameter name
Returns: value or empty string if param not exists
func (*RestOperations) SendBadRequest ¶
func (c *RestOperations) SendBadRequest(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SendConflict ¶
func (c *RestOperations) SendConflict(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SendCreatedResult ¶
func (c *RestOperations) SendCreatedResult(res http.ResponseWriter, req *http.Request, result any, err error)
func (*RestOperations) SendDeletedResult ¶
func (c *RestOperations) SendDeletedResult(res http.ResponseWriter, req *http.Request, result any, err error)
func (*RestOperations) SendEmptyResult ¶
func (c *RestOperations) SendEmptyResult(res http.ResponseWriter, req *http.Request, err error)
func (*RestOperations) SendError ¶
func (c *RestOperations) SendError(res http.ResponseWriter, req *http.Request, err error)
func (*RestOperations) SendInternalError ¶
func (c *RestOperations) SendInternalError(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SendNotFound ¶
func (c *RestOperations) SendNotFound(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SendResult ¶
func (c *RestOperations) SendResult(res http.ResponseWriter, req *http.Request, result any, err error)
func (*RestOperations) SendServerUnavailable ¶
func (c *RestOperations) SendServerUnavailable(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SendSessionExpired ¶
func (c *RestOperations) SendSessionExpired(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SendUnauthorized ¶
func (c *RestOperations) SendUnauthorized(res http.ResponseWriter, req *http.Request, message string)
func (*RestOperations) SetReferences ¶
func (c *RestOperations) SetReferences(ctx context.Context, references crefer.IReferences)
SetReferences method are sets references to this RestOperations logger, counters, and connection resolver.
Parameters: - ctx context.Context - references an IReferences object, containing references to a logger, counters, and a dependency resolver.
type RestService ¶
type RestService struct { Overrides IRegisterable //The base route. BaseRoute string //The HTTP endpoint that exposes this service. Endpoint *HttpEndpoint //The dependency resolver. DependencyResolver *crefer.DependencyResolver //The logger. Logger *clog.CompositeLogger //The performance counters. Counters *ccount.CompositeCounters // The tracer. Tracer *ctrace.CompositeTracer SwaggerService ISwaggerService SwaggerEnabled bool SwaggerRoute string // contains filtered or unexported fields }
RestService Abstract service that receives remove calls via HTTP/REST protocol.
Configuration parameters: - base_route: base route for remote URI - dependencies: - endpoint: override for HTTP Endpoint dependency - controller: override for Controller dependency - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - protocol: connection protocol: http or https - host: host name or IP address - port: port number - uri: resource URI or connection string with all parameters in it - credential - the HTTPS credentials: - ssl_key_file: the SSL private key in PEM - ssl_crt_file: the SSL certificate in PEM - ssl_ca_file: the certificate authorities (root cerfiticates) in PEM References: - *:logger:*:*:1.0 (optional) ILogger components to pass log messages - *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection - *:endpoint:http:*:1.0 (optional) HttpEndpoint reference See clients.RestClient Example: type MyRestService struct { *RestService controller IMyController } ... func NewMyRestService() *MyRestService { c := MyRestService{} c.RestService = services.NewRestService() c.RestService.IRegisterable = &c c.numberOfCalls = 0 c.DependencyResolver.Put(context.Background(), "controller", crefer.NewDescriptor("mygroup", "controller", "*", "*", "1.0")) return &c } func (c * MyRestService) SetReferences(ctx context.Context, references IReferences) { c.RestService.SetReferences(ctx, references); resolv := c.DependencyResolver.GetRequired("controller"); if resolv != nil { c.controller, _ = resolv.(IMyController) } } func (c *MyRestService) getOneById(res http.ResponseWriter, req *http.Request) { params := req.URL.Query() vars := mux.Vars(req) mydataId := params.Get("mydata_id") if mydataId == "" { mydataId = vars["mydatay_id"] } result, err := c.controller.GetOneById(params.Get("correlation_id"), mydataId), c.SendResult(res, req, result, err) } func (c * MyRestService) Register() { c.RegisterRoute( "get", "get_mydata/{mydata_id}", &cvalid.NewObjectSchema(). WithRequiredProperty("mydata_id", cconv.String).Schema, c.getOneById, ) ... } service := NewMyRestService(); service.Configure(context.Background(), cconf.NewConfigParamsFromTuples( "connection.protocol", "http", "connection.host", "localhost", "connection.port", 8080, )); service.SetReferences(context.Background(), cref.NewReferencesFromTuples( cref.NewDescriptor("mygroup","controller","default","default","1.0"), controller )); opnRes := service.Open(context.Background(), "123") if opnErr == nil { fmt.Println("The REST service is running on port 8080"); }
func InheritRestService ¶
func InheritRestService(overrides IRegisterable) *RestService
InheritRestService creates new instance of RestService
func (*RestService) Close ¶
func (c *RestService) Close(ctx context.Context, correlationId string) error
Close method are closes component and frees used resources.
Parameters: - ctx context.Context - correlationId (optional) transaction id to trace execution through call chain. Returns: error or nil no errors occurred.
func (*RestService) Configure ¶
func (c *RestService) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure method are configures component by passing configuration parameters.
Parameters: - ctx context.Context - config *cconf.ConfigParams configuration parameters to be set.
func (*RestService) DecodeBody ¶
func (c *RestService) DecodeBody(req *http.Request, target any) error
DecodeBody methods helps decode body
Parameters: - req - incoming request - target - pointer on target variable for decode
Returns error
func (*RestService) GetCorrelationId ¶
func (c *RestService) GetCorrelationId(req *http.Request) string
GetCorrelationId method returns CorrelationId from request
Parameters: - req *http.Request request Returns: string correlation_id or empty string
func (*RestService) GetFilterParams ¶
func (c *RestService) GetFilterParams(req *http.Request) *cdata.FilterParams
GetFilterParams methods helps decode filter params
Parameters: - req incoming request Returns: filter params
func (*RestService) GetPagingParams ¶
func (c *RestService) GetPagingParams(req *http.Request) *cdata.PagingParams
GetPagingParams methods helps decode paging params
Parameters: - req incoming request Returns: paging params
func (*RestService) GetParam ¶
func (c *RestService) GetParam(req *http.Request, name string) string
GetParam methods helps get all params from query
Parameters: - req incoming request - name parameter name Returns value or empty string if param not exists
func (*RestService) Instrument ¶
func (c *RestService) Instrument(ctx context.Context, correlationId string, name string) *InstrumentTiming
Instrument method are adds instrumentation to log calls and measure call time. It returns a Timing object that is used to end the time measurement.
Parameters: - ctx context.Context - correlationId (optional) transaction id to trace execution through call chain. - name a method name. Returns: Timing object to end the time measurement.
func (*RestService) InstrumentError ¶
func (c *RestService) InstrumentError(ctx context.Context, correlationId string, name string, errIn error, resIn any) (result any, err error)
InstrumentError method are adds instrumentation to error handling.
Parameters: - ctx context.Context - correlationId string (optional) transaction id to trace execution through call chain. - name string a method name. - err error an occurred error - result any (optional) an execution result Returns: result any, err error (optional) an execution callback
func (*RestService) IsOpen ¶
func (c *RestService) IsOpen() bool
IsOpen method checks if the component is opened.
Returns: true if the component has been opened and false otherwise.
func (*RestService) Open ¶
func (c *RestService) Open(ctx context.Context, correlationId string) error
Open method are opens the component.
Parameters: - ctx context.Context - correlationId string (optional) transaction id to trace execution through call chain. Returns: error or nil no errors occured.
func (*RestService) Register ¶
func (c *RestService) Register()
Register method are registers all service routes in HTTP endpoint.
func (*RestService) RegisterInterceptor ¶
func (c *RestService) RegisterInterceptor(route string, action func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc))
RegisterInterceptor method are registers a middleware for a given route in HTTP endpoint.
Parameters: - route a command route. Base route will be added to this route - action an action function that is called when middleware is invoked.
func (*RestService) RegisterOpenApiSpec ¶
func (c *RestService) RegisterOpenApiSpec(content string)
func (*RestService) RegisterOpenApiSpecFromFile ¶
func (c *RestService) RegisterOpenApiSpecFromFile(path string)
func (*RestService) RegisterRoute ¶
func (c *RestService) RegisterRoute(method string, route string, schema *cvalid.Schema, action func(res http.ResponseWriter, req *http.Request))
RegisterRoute method are registers a route in HTTP endpoint.
Parameters: - method HTTP method: "get", "head", "post", "put", "delete" - route a command route. Base route will be added to this route - schema a validation schema to validate received parameters. - action an action function that is called when operation is invoked.
func (*RestService) RegisterRouteWithAuth ¶
func (c *RestService) RegisterRouteWithAuth(method string, route string, schema *cvalid.Schema, authorize func(res http.ResponseWriter, req *http.Request, next http.HandlerFunc), action func(res http.ResponseWriter, req *http.Request))
RegisterRouteWithAuth method are registers a route with authorization in HTTP endpoint.
Parameters: - method HTTP method: "get", "head", "post", "put", "delete" - route a command route. Base route will be added to this route - schema a validation schema to validate received parameters. - authorize an authorization interceptor - action an action function that is called when operation is invoked.
func (*RestService) SendCreatedResult ¶
func (c *RestService) SendCreatedResult(res http.ResponseWriter, req *http.Request, result any, err error)
SendCreatedResult method are sends newly created object as JSON. That callback function call be called directly or passed as a parameter to business logic components. If object is not nil it returns 201 status code. For nil results it returns 204 status code. If error occur it sends ErrorDescription with approproate status code.
Parameters: - req a HTTP request object. - res a HTTP response object. - result (optional) result object to send - err error (optional) error objrct to send
func (*RestService) SendDeletedResult ¶
func (c *RestService) SendDeletedResult(res http.ResponseWriter, req *http.Request, result any, err error)
SendDeletedResult method are sends deleted object as JSON. That callback function call be called directly or passed as a parameter to business logic components. If object is not nil it returns 200 status code. For nil results it returns 204 status code. If error occur it sends ErrorDescription with appropriate status code.
Parameters: - req a HTTP request object. - res a HTTP response object. - result (optional) result object to send - err error (optional) error objrct to send
func (*RestService) SendError ¶
func (c *RestService) SendError(res http.ResponseWriter, req *http.Request, err error)
SendError method are sends error serialized as ErrorDescription object and appropriate HTTP status code. If status code is not defined, it uses 500 status code.
Parameters: - req a HTTP request object. - res a HTTP response object. - error an error object to be sent.
func (*RestService) SendResult ¶
func (c *RestService) SendResult(res http.ResponseWriter, req *http.Request, result any, err error)
SendResult method are sends result as JSON object. That function call be called directly or passed as a parameter to business logic components. If object is not nil it returns 200 status code. For nil results it returns 204 status code. If error occur it sends ErrorDescription with approproate status code.
Parameters: - req a HTTP request object. - res a HTTP response object. - result (optional) result object to send - err error (optional) error objrct to send
func (*RestService) SetReferences ¶
func (c *RestService) SetReferences(ctx context.Context, references crefer.IReferences)
SetReferences method are sets references to dependent components.
Parameters: - ctx context.Context - references crefer.IReferences references to locate the component dependencies.
func (*RestService) UnsetReferences ¶
func (c *RestService) UnsetReferences()
UnsetReferences method are unsets (clears) previously set references to dependent components.
type StatusOperations ¶
type StatusOperations struct { *RestOperations // contains filtered or unexported fields }
StatusOperations helper class for status service
func NewStatusOperations ¶
func NewStatusOperations() *StatusOperations
NewStatusOperations creates new instance of StatusOperations
func (*StatusOperations) GetStatusOperation ¶
func (c *StatusOperations) GetStatusOperation() func(res http.ResponseWriter, req *http.Request)
GetStatusOperation return function for get status
func (*StatusOperations) SetReferences ¶
func (c *StatusOperations) SetReferences(ctx context.Context, references crefer.IReferences)
SetReferences sets references to dependent components.
Parameters: - ctx context.Context - references crefer.IReferences references to locate the component dependencies.
func (*StatusOperations) Status ¶
func (c *StatusOperations) Status(res http.ResponseWriter, req *http.Request)
Status method handles status requests
Parameters: - req *http.Request an HTTP request - res http.ResponseWriter an HTTP response
type StatusRestService ¶
type StatusRestService struct { *RestService // contains filtered or unexported fields }
StatusRestService is a service that returns microservice status information via HTTP/REST protocol.
The service responds on /status route (can be changed) with a JSON object: { "id": unique container id (usually hostname) "name": container name (from ContextInfo) "description": container description (from ContextInfo) "start_time": time when container was started "current_time": current time in UTC "uptime": duration since container start time in milliseconds "properties": additional container properties (from ContextInfo) "components": descriptors of components registered in the container } Configuration parameters: - baseroute: base route for remote URI - route: status route (default: "status") - dependencies: - endpoint: override for HTTP Endpoint dependency - controller: override for Controller dependency - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - protocol: connection protocol: http or https - host: host name or IP address - port: port number - uri: resource URI or connection string with all parameters in it References: - *:logger:*:*:1.0 (optional) ILogger components to pass log messages - *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements - *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connection - *:endpoint:http:*:1.0 (optional) HttpEndpoint reference see: RestService see: clients.RestClient Example: service = NewStatusService(); service.Configure(context.Background(), cref.NewConfigParamsFromTuples( "connection.protocol", "http", "connection.host", "localhost", "connection.port", 8080, )); opnErr:= service.Open(context.Background(), "123") if opnErr == nil { fmt.Println("The Status service is accessible at http://localhost:8080/status"); }
func NewStatusRestService ¶
func NewStatusRestService() *StatusRestService
NewStatusRestService method are creates a new instance of this service.
func (*StatusRestService) Configure ¶
func (c *StatusRestService) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure method are configures component by passing configuration parameters.
Parameters: - ctx context.Context - config *cconf.ConfigParams configuration parameters to be set.
func (*StatusRestService) Register ¶
func (c *StatusRestService) Register()
Register method are registers all service routes in HTTP endpoint.
func (*StatusRestService) SetReferences ¶
func (c *StatusRestService) SetReferences(ctx context.Context, references crefer.IReferences)
SetReferences method are sets references to dependent components.
Parameters: - ctx context.Context - references crefer.IReferences references to locate the component dependencies.
Source Files ¶
- AboutOperations.go
- CommandableHttpService.go
- CommandableSwaggerDocument.go
- HeartbeatOperations.go
- HeartbeatRestService.go
- HttpEndpoint.go
- HttpRequestDetector.go
- HttpResponseSender.go
- IRegisterable.go
- ISwaggerService.go
- InstrumentTiming.go
- RestOperations.go
- RestService.go
- StatusOperations.go
- StatusRestService.go