CloudFunctionService

Abstract service that receives remove calls via the Google Function protocol.

Description

The CloudFunctionService class allows you to create a service that receives remove calls via the Google Function protocol.

Important points

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

Configuration parameters

  • dependencies:
    • controller: override for Controller dependency

References

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

Constructors

NewCloudFunctionService

Creates an instance of this service.

NewCloudFunctionService(name string) *CloudFunctionService

  • name: string - name of the service used to generate an action cmd.

InheritCloudFunctionService

InheritCloudFunctionService(overrides ICloudFunctionServiceOverrides, name string) *CloudFunctionService

Fields

Counters

Performance counters.

Counters: *CompositeCounters

DependencyResolver

Dependency resolver.

DependencyResolver: *DependencyResolver

Logger

Logger.

Logger: *CompositeLogger

Tracer

Tracer.

Tracer: *CompositeTracer

Instance methods

ApplyInterceptors

Applies interceptors to the action.

(c *CloudFunctionService) ApplyInterceptors(action http.HandlerFunc) http.HandlerFunc

  • action: http.HandlerFunc - action
  • returns: http.HandlerFunc - returned result

ApplyValidation

Performs a validation.

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

  • schema: *Schema - schema used in the validation
  • action: http.HandlerFunc - action
  • returns: http.HandlerFunc - returned result

Close

Closes a component and frees used resources.

(c *CloudFunctionService) Close(ctx context.Context, correlationId string) error

  • ctx: context.Context - operation context.
  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • returns: error - close error.

Configure

Configures a component by passing its configuration parameters.

(c *CloudFunctionService) Configure(ctx context.Context, config *ConfigParams)

  • ctx: context.Context - operation context.
  • config: *ConfigParams - configuration parameters to be set.

GenerateActionCmd

Adds ‘.cmd’ to a command name

(c *CloudFunctionService) GenerateActionCmd(name string) string

  • name: string - command name
  • returns: string - command name with ‘.cmd’ added at its end.

GetActions

Get all actions supported by the service.

(c *CloudFunctionService) GetActions() []*CloudFunctionAction

GetCommand

Returns command from Google Function request.

This method can be overloaded in child classes

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

  • r: *http.Request - the function request
  • returns: (string, error) - returned command from request.

GetCorrelationId

Returns correlationId from Google Function request.

This method can be overloaded in child classes

(c *CloudFunctionService) GetCorrelationId(r *http.Request) string

  • r: *http.Request - the function request
  • returns: string - returned correlationId from request.

Instrument

Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.

(c *CloudFunctionService) Instrument(ctx context.Context, correlationId string, name string) *InstrumentTiming

  • ctx: context.Context - operation context.
  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • name: string - method’s name.
  • returns: *InstrumentTiming - Timing object to end the time measurement.

IsOpen

Checks if the component is open.

IsOpen() bool

  • returns: bool - true if the component is open and false otherwise.

Open

Opens the component.

(c *CloudFunctionService) Open(ctx context.Context, correlationId string) error

  • ctx: context.Context - operation context.
  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • retiurns: error - open error.

Register

Registers all service routes in an HTTP endpoint.

This method is called by the service and must be overridden in child classes.

Register()

RegisterAction

Registers an action in Google Function function.

(c *CloudFunctionService) RegisterAction(name string, schema *Schema, action http.HandlerFunc)

  • name: string - action name
  • schema: *Schema - validation schema used to validate received parameters.
  • action: http.HandlerFunc - action function that is called when the operation is invoked.

RegisterActionWithAuth

Registers an action with authorization.

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

  • name: string - action’s name
  • schema: *Schema - validation schema used to validate received parameters.
  • authorize: func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) - authorization interceptor
  • action: http.HandlerFunc - action function that is called when the operation is invoked.

RegisterInterceptor

Registers a middleware for actions in Google Function service.

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

  • cmd: command or regex for intercept.
  • action: func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) - action function that is called when middleware is invoked.

SetReferences

Sets references to dependent components.

(c *CloudFunctionService) SetReferences(ctx context.Context, references IReferences)

  • ctx: context.Context - operation context.
  • references: IReferences - references to locate the component’s dependencies.

Examples

type MyCloudFunctionService struct {
	*services.CloudFunctionService
	controller IMyController
}

func NewMyCloudFunctionService() *MyCloudFunctionService {
	c := MyCloudFunctionService{}

	c.CloudFunctionService = services.InheritCloudFunctionService(&c, "v1.myservice")
	c.DependencyResolver.Put(context.Background(), "controller", refer.NewDescriptor("mygroup", "controller", "default", "*", "1.0"))

	return &c
}

func (c *MyCloudFunctionService) SetReferences(ctx context.Context, references refer.IReferences) {
	c.CloudFunctionService.SetReferences(ctx, references)
	depRes, depErr := c.DependencyResolver.GetOneRequired("controller")

	if depErr == nil && depRes != nil {
		c.controller = depRes.(IMyController)
	}
}

func (c *MyCloudFunctionService) 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.controller.DeleteById(
				r.Context(),
				c.GetCorrelationId(r),
				body,
			)
			HttpResponseSender.SendDeletedResult(w, r, result, err)
		},
	)
}

...

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

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