services

package
v1.1.0-3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloudFunctionAction

type CloudFunctionAction struct {
	// Command to call the action
	Cmd string
	// Schema to validate action parameters
	Schema *cvalid.Schema
	// Action to be executed
	Action http.HandlerFunc
}

type CloudFunctionController

type CloudFunctionController struct {
	Overrides ICloudFunctionControllerOverrides
	// The dependency resolver.
	DependencyResolver *crefer.DependencyResolver
	// The logger.
	Logger *clog.CompositeLogger
	// The performance counters.
	Counters *ccount.CompositeCounters
	// The tracer.
	Tracer *ctrace.CompositeTracer
	// contains filtered or unexported fields
}

Abstract service that receives remove calls via Google Function protocol.

This service is intended to work inside CloudFunction container that exposes registered actions externally.

Configuration parameters
	- dependencies:
		- service:	override for Service dependency

References
	- *:logger:*:*:1.0			(optional) ILogger components to pass log messages
	- *:counters:*:*:1.0		(optional) ICounters components to pass collected measurements

Example:
	type MyCloudFunctionController struct {
		*services.CloudFunctionController
		service IMyService
	}

	func NewMyCloudFunctionController() *MyCloudFunctionController {
		c := MyCloudFunctionController{}

		c.CloudFunctionController = services.InheritCloudFunctionController(&c, "v1.mycontroller")
		c.DependencyResolver.Put(context.Background(), "service", refer.NewDescriptor("mygroup", "controller", "default", "*", "1.0"))

		return &c
	}

	func (c *MyCloudFunctionController) SetReferences(ctx context.Context, references refer.IReferences) {
		c.CloudFunctionController.SetReferences(ctx, references)
		depRes, depErr := c.DependencyResolver.GetOneRequired("service")

		if depErr == nil && depRes != nil {
			c.service = depRes.(IMyService)
		}
	}

	func (c *MyCloudFunctionController) Register() {
		c.RegisterAction(
			"get_mydata",
			nil,
			func(w http.ResponseWriter, r *http.Request) {
				var body map[string]any

				err := CloudFunctionRequestHelper.DecodeBody(r, &body)
				defer r.Body.Close()

				result, err := c.service.DeleteById(
					cctx.NewContextWithTraceId(r.Context(), c.GetTraceId(c)),
					body,
				)
				HttpResponseSender.SendDeletedResult(w, r, result, err)
			},
		)
	}

	...

	controller := NewMyCloudFunctionController()
	controller.Configure(ctx, config.NewConfigParamsFromTuples(
		"connection.protocol", "http",
		"connection.host", "localhost",
		"connection.port", 8080,
	))

	controller.SetReferences(ctx, refer.NewReferencesFromTuples(
		refer.NewDescriptor("mygroup", "service", "default", "default", "1.0"), service,
	))
	controller.Open(ctx, "123")
	fmt.Println("The Google Function controller is running")

func InheritCloudFunctionController

func InheritCloudFunctionController(overrides ICloudFunctionControllerOverrides, name string) *CloudFunctionController

InheritCloudFunctionController creates new instance of CloudFunctionService

func NewCloudFunctionService

func NewCloudFunctionService(name string) *CloudFunctionController

Creates an instance of this service. Parameters:

  • name a service name to generate action cmd.

func (*CloudFunctionController) ApplyInterceptors

func (c *CloudFunctionController) ApplyInterceptors(action http.HandlerFunc) http.HandlerFunc

func (*CloudFunctionController) ApplyValidation

func (c *CloudFunctionController) ApplyValidation(schema *cvalid.Schema, action http.HandlerFunc) http.HandlerFunc

func (*CloudFunctionController) Close

Close method are closes component and frees used resources.

Parameters:
	- ctx context.Context a context to trace execution through call chain.
Returns: error or nil no errors occurred.

func (*CloudFunctionController) Configure

func (c *CloudFunctionController) Configure(ctx context.Context, config *cconf.ConfigParams)

Configure the component with specified parameters.

see ConfigParams
Parameters:
	- ctx context.Context
	- config *conf.ConfigParams configuration parameters to set.

func (*CloudFunctionController) GenerateActionCmd

func (c *CloudFunctionController) GenerateActionCmd(name string) string

func (*CloudFunctionController) GetActions

func (c *CloudFunctionController) GetActions() []*CloudFunctionAction

Get all actions supported by the service. Returns an array with supported actions.

func (*CloudFunctionController) GetCommand

func (c *CloudFunctionController) GetCommand(r *http.Request) (string, error)

Returns command from Google Function request. This method can be overloaded in child structs. Parameters:

  • req the function request

Returns command from request

func (*CloudFunctionController) GetTraceId

func (c *CloudFunctionController) GetTraceId(r *http.Request) string

Returns traceId from Google Function request. This method can be overloaded in child structs

func (*CloudFunctionController) Instrument

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 a context to trace execution through call chain.
	- name              a method name.
Returns: Timing object to end the time measurement.

func (*CloudFunctionController) IsOpen

func (c *CloudFunctionController) IsOpen() bool

IsOpen Checks if the component is opened.

Returns: bool true if the component has been opened and false otherwise.

func (*CloudFunctionController) Open

Open method are opens the component.

Parameters:
	- ctx context.Context a context to trace execution through call chain.
Returns: error or nil no errors occured.

func (*CloudFunctionController) Register

func (c *CloudFunctionController) Register()

Registers all service routes in HTTP endpoint. This method is called by the service and must be overridden in child structs.

func (*CloudFunctionController) RegisterAction

func (c *CloudFunctionController) RegisterAction(name string, schema *cvalid.Schema, action http.HandlerFunc)

Registers a action in Google Function function. Parameters:

  • name an action name
  • schema a validation schema to validate received parameters.
  • action an action function that is called when operation is invoked.

func (*CloudFunctionController) RegisterActionWithAuth

func (c *CloudFunctionController) RegisterActionWithAuth(name string, schema *cvalid.Schema, authorize func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc),
	action http.HandlerFunc)

Registers an action with authorization. Parameters:

  • name an action name
  • schema a validation schema to validate received parameters.
  • authorize an authorization interceptor
  • action an action function that is called when operation is invoked.

func (*CloudFunctionController) RegisterInterceptor

func (c *CloudFunctionController) RegisterInterceptor(cmd string, action func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc))

Registers a middleware for actions in Google Function service. Parameters:

  • action an action function that is called when middleware is invoked.

func (*CloudFunctionController) SetReferences

func (c *CloudFunctionController) SetReferences(ctx context.Context, references crefer.IReferences)

SetReferences sets references to dependent components.

see IReferences
Parameters:
	- ctx context.Context
	- references IReferences references to locate the component dependencies.

type CommandableCloudFunctionController

type CommandableCloudFunctionController struct {
	*CloudFunctionController
	// contains filtered or unexported fields
}

Abstract service that receives commands via Google Function protocol to operations automatically generated for commands defined in ccomand.ICommandable components. Each command is exposed as invoke method that receives command name and parameters.

Commandable services require only 3 lines of code to implement a robust external Google Function-based remote interface.

This service is intended to work inside Google Function container that exploses registered actions externally.

Configuration parameters:
	- dependencies:
		- service:            override for Service dependency
References
	- *:logger:*:*:1.0			(optional) ILogger components to pass log messages
	- *:counters:*:*:1.0		(optional) ICounters components to pass collected measurements

see CloudFunctionService

Example:
	type MyCommandableCloudFunctionController struct {
		*gcpsrv.CommandableCloudFunctionController
	}

	func NewMyCommandableCloudFunctionController() *MyCommandableCloudFunctionController {
		c := MyCommandableCloudFunctionController{}
		c.CommandableCloudFunctionController = gcpsrv.NewCommandableCloudFunctionService("mydata")
		c.DependencyResolver.Put(context.Background(), "service", crefer.NewDescriptor("mygroup", "service", "default", "*", "*"))
		return &c
	}

/ ...

service := NewMyCommandableCloudFunctionController()
service.SetReferences(crefer.NewReferencesFromTuples(
	crefer.NewDescriptor("mygroup","controller","default","default","1.0"), controller,
))
service.Open(ctx, "123")
fmt.Println("The Google Function service is running")

func NewCommandableCloudFunctionController

func NewCommandableCloudFunctionController(name string) *CommandableCloudFunctionController

Creates a new instance of the service. Parameters:

  • name a service name.

func (*CommandableCloudFunctionController) GetParameters

Returns body from Google Function request. This method can be overloaded in child classes Parameters:

  • req Google Function request

Returns Parameters from request

func (*CommandableCloudFunctionController) Register

func (c *CommandableCloudFunctionController) Register()

Registers all actions in Google Function.

type ICloudFunctionController

type ICloudFunctionController interface {

	// Get all actions supported by the service.
	// Returns an array with supported actions.
	GetActions() []*CloudFunctionAction
}

An interface that allows to integrate Google Function services into Google Function containers and connect their actions to the function calls.

type ICloudFunctionControllerOverrides

type ICloudFunctionControllerOverrides interface {
	Register()
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL