Implements: ICloudFunctionController, IOpenable, IConfigurable, IReferenceable
Description
The CloudFunctionController 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
Creates an instance of this service.
publicconstructor(name?: string)
- name: string - name of the service used to generate an action cmd.
Fields
Instance methods
applyInterceptors
Applies interceptors to the action.
protectedapplyInterceptors(action: (req: Request, res: Response) => Promise<any>): (req: Request, res: Response) => Promise<any>
- action: (req: Request, res: Response) => Promise<any> - action
- returns: (req: Request, res: Response) => Promise<any> - returned result
applyValidation
Performs a validation.
protectedapplyValidation(schema: Schema, action: (req: Request, res: Response) => Promise<any>): (req: Request, res: Response) => Promise<any>
- schema: Schema - schema used in the validation
- action: (req: Request, res: Response) => Promise<any> - action
- returns: (req: Request, res: Response) => Promise<any> - returned result
close
Closes a component and frees used resources.
publicclose(correlationId: string): Promise<void>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
configure
Configures a component by passing its configuration parameters.
publicconfigure(config: ConfigParams): void
- config: ConfigParams - configuration parameters to be set.
generateActionCmd
Adds ‘.cmd’ to a command name
protectedgenerateActionCmd(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.
publicgetActions(): CloudFunctionAction[]
- returns: CloudFunctionAction[] - array with supported actions.
getCommand
Returns command from Google Function request.
This method can be overloaded in child classes
protectedgetCommand(req: any): string
- req: any - the function request
- returns: string - returned command from request.
getCorrelationId
Returns correlationId from Google Function request.
This method can be overloaded in child classes
protectedgetCorrelationId(req: any): string
- req: any - 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.
protectedinstrument(correlationId: string, name: string): InstrumentTiming
- 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.
publicisOpen(): boolean
- returns: boolean - true if the component is open and false otherwise.
open
Opens the component.
publicopen(correlationId: string): Promise<void>
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
registerAction
Registers an action in Google Function function.
protectedregisterAction(name: string, schema: Schema, action: (req: Request, res: Response) => Promise<any>): void
- name: string - action name
- schema: Schema - validation schema used to validate received parameters.
- action: (req: Request, res: Response) => Promise<any> - action function that is called when the operation is invoked.
registerActionWithAuth
Registers an action with authorization.
protectedregisterActionWithAuth(name: string, schema: Schema, authorize: (req: Request, res: Response, next: (req: Request, res: Response) => Promise<any>) => Promise<any>, action: (req: Request, res: Response) => Promise<any>): void
- name: string - action’s name
- schema: Schema - validation schema used to validate received parameters.
- authorize: (req: Request, res: Response, next: (req: Request, res: Response) => Promise<any>) => Promise<any> - authorization interceptor
- action: (req: Request, res: Response) => Promise<any> - action function that is called when the operation is invoked.
registerInterceptor
Registers a middleware for actions in Google Function service.
protectedregisterInterceptor(action: (req: Request, res: Response, next: (req: Request, res: Response) => Promise<any>) => Promise<any>): void
- action: (req: Request, res: Response, next: (req: Request, res: Response) => Promise<any>) => Promise<any> - action function that is called when middleware is invoked.
setReferences
Sets references to dependent components.
publicsetReferences(references: IReferences): void
- references: IReferences - references to locate the component’s dependencies.
Abstract methods
register
Registers all service routes in an HTTP endpoint.
This method is called by the service and must be overridden in child classes.
protected abstractregister(): void
Examples
class MyCloudFunctionController extends CloudFunctionController {
private _controller: IMyController;
...
public constructor() {
base('v1.myservice');
this._dependencyResolver.put(
"controller",
new Descriptor("mygroup","controller","*","*","1.0")
);
}
public setReferences(references: IReferences): void {
base.setReferences(references);
this._controller = this._dependencyResolver.getRequired<IMyController>("controller");
}
public register(): void {
registerAction("get_mydata", null, async (req, res) => {
let params = req.body;
let traceId = params.trace_id;
let id = params.id;
const result = await this._controller.getMyData(traceId, id);
res.send(result);
});
...
}
}
let service = new MyCloudFunctionController();
service.configure(ConfigParams.fromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
));
service.setReferences(References.fromTuples(
new Descriptor("mygroup","controller","default","default","1.0"), controller
));
service.open("123_service");