AzureFunction

Abstract Azure Function that acts as a container to instantiate and run components and expose them via an external entry point.

Description

The AzureFunction class allows you to create an Abstract Azure Function that acts as a container to instantiate and run components and expose them via an external entry point.

Importan points

  • When handling calls, the “cmd” parameter determines the action that shall be called, while the other parameters are passed to the action itself.

  • The container configuration for this Azure Function is stored in "./config/config.yml" file. But this path can be overriden by CONFIG_PATH environment variable.

References

  • *:logger:*:*:1.0: (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements
  • *:service:azure-function:*:1.0: (optional) IAzureFunctionController services to handle action requests.
  • *:service:commandable-azure-function:*:1.0: (optional) IAzureFunctionController services to handle action requests.

Constructors

NewAzureFunctionWithParams

Creates a new instance of this Azure Function.

NewAzureFunctionWithParams(name string, description string) *AzureFunction

  • name: string - (optional) container’s name (accessible via ContextInfo)
  • description: string - (optional) container’s description (accessible via ContextInfo)

NewAzureFunction

Creates a new instance of this Azure Function function.

NewAzureFunction() *AzureFunction

InheritAzureFunction

InheritAzureFunction creates new instance of AzureFunction

InheritAzureFunction(overrides IAzureFunctionOverrides) *AzureFunction

InheritAzureFunction

InheritAzureFunction creates new instance of AzureFunction

InheritAzureFunctionWithParams(overrides IAzureFunctionOverrides, name string, description string) *AzureFunction

  • overrides: IAzureFunctionOverrides - instance thath overrides methods.
  • name: string - (optional) container’s name (accessible via ContextInfo)
  • description: string - (optional) container’s description (accessible via ContextInfo)

Fields

Actions

The map of registered actions.

protected Actions: map[string]http.HandlerFunc

configPath

The default path to config file.

configPath: string = ‘./config/config.yml’

Counters

Performance counters.

Counters: *CompositeCounters

DependencyResolver

Dependencies resolver.

DependencyResolver: *DependencyResolver

Tracer

The tracer.

Tracer: *CompositeTracer

Schemas

The map of registred validation schemas.

Schemas: map[string]*Schema

Instance methods

Execute

Executes this Azure Function and returns the result. This method can be overloaded in child classes if they need to change the default behavior

(c *AzureFunction) Execute(res http.ResponseWriter, req *http.Request)

  • res: http.ResponseWriter - context the context parameters (or function arguments)
  • req: *http.Request - the result of the function execution.

GetCommand

Returns command from Azure Function context. This method can be overloaded in child classes

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

  • r: *http.Request - Azure Function context
  • returns: (string, error) - Returns command from context

GetTraceId

Returns traceId from Azure Function context. This method can be overloaded in child classes

(c *AzureFunction) GetTraceId(r *http.Request) string

  • r: *http.Request - Azure Function context
  • returns: string - Returns traceId from context

GetHandler

Return plugin function

(c *AzureFunction) GetHandler() http.HandlerFunc

  • returns: http.HandlerFunc - plugin function

Instrument

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

(c *AzureFunction) Instrument(ctx context.Context, context IContext, name string) *InstrumentTiming

  • context: IContext - (optional) a context to trace execution through a call chain.
  • name: string - method’s name.
  • returns: *InstrumentTiming - Timing object to end the time measurement.

Open

Opens the component.

(c *AzureFunction) Open(ctx context.Context, context IContext) error

  • ctx: context.Context - operation context.
  • context: IContext - (optional) a context to trace execution through a call chain.

RegisterAction

Registers an action in this Azure Function.

Note: This method has been deprecated. Use AzureFunctionService instead.

(c *AzureFunction) RegisterAction(cmd string, schema *Schema, action http.HandlerFunc)

  • cmd: string - a action/command name.
  • schema: *Schema - a validation schema to validate received parameters.
  • action: http.HandlerFunc - an action function that is called when action is invoked.

Register

Registers all actions in this Azure Function.

Note: Overloading of this method has been deprecated. Use AzureFunctionService instead.

(c *AzureFunction) Register()

RegisterServices

Registers all Azure Function services in the container.

(c *AzureFunction) RegisterServices()

Run

Runs this Azure Function, loads container configuration, instantiate components and manage their lifecycle, makes this function ready to access action calls.

(c *AzureFunction) Run(ctx context.Context)

  • ctx: context.Context - operation context.

SetReferences

Sets references to dependent components.

SetReferences(ctx context.Context, references IReferences)

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

Examples

type MyAzureFunction struct {
	*containers.AzureFunction
}

func NewMyAzureFunction() *MyAzureFunction {
	c := MyAzureFunction{}
	c.AzureFunction = containers.NewAzureFunctionWithParams("mygroup", "MyGroup Azure Function")

	return &c
}

...

AzureFunction := NewMyAzureFunction()
AzureFunction.Run(ctx)
fmt.Println("MyAzureFunction is started")