For Golang functions, veFaaS provides a Runtime SDK for you to develop your functions. You need to import the Runtime SDK into your local code. veFaaS uses the entry function exposed by the Runtime SDK to start the veFaaS process and handle HTTP requests or Event requests.
Startup function
Currently, veFaaS provides two function startup entry points: Start(handler interface{}) and StartWithInitializer(handler interface{}, initializer interface{}). Select the appropriate startup function based on your actual needs.
Start function
In general scenarios, you can choose the Start function as the startup function.
You need to provide a handler function as an input parameter to this function. The handler function is used to process the specific business logic. For a detailed introduction to the handler function, see handler function section below in this topic. The following is a Golang code example that uses the Start function to start the service.
StartWithInitializer function
If your function has some global initialization logic, such as loading configuration files, establishing a connection to a database, or initializing downstream clients, we recommend using the StartWithInitializer function as the startup function.
You need to provide two parameters to this function.
- handler function: It is used to process the specific business logic. For a detailed introduction to the handler function, see handler function section below in this topic.
- initializer function: It is used for initializing function instances. For details about the initializer function, see initializer function section below in this topic.
The following is a Golang code example that uses the StartWithInitializer function to start the service.
handler function
The handler is the entry point for the function's request processing logic. The handler contains the user's core business logic. It is used to receive HTTP or event-type requests, process them, and ultimately return a response.
Based on the type of request received, the handler function signature can be categorized into the following three types. Use the appropriate signature based on your actual needs.
Handling regular HTTP requests
The function signature for this type is as follows. It receives an HTTP request and the corresponding Context, and returns an EventResponse along with any possible Error that occurs during request processing. This is suitable for scenarios where veFaaS functions are invoked via API Gateway triggers.
Parameter description
Handling CloudEvent requests
The signature for this type is shown below. It receives a CloudEvent-format request encapsulated by veFaaS and the corresponding Context, and returns an EventResponse along with any possible Error that occurs during request processing. This is suitable for handling trigger scenarios of event types such as Timer triggers and Kafka triggers.
Parameter description
- context.Context, events.EventResponse, and error parameter descriptions are the same as those for handler functions processing regular HTTP requests.
- For descriptions of the remaining parameters, see the table below.
Handling arbitrary types of requests
The signature for this type is shown below. It is suitable for scenarios where a single function needs to receive both regular HTTP requests and event-type requests. Inside the handler function, you can further distinguish the request type using Golang's type assertion and handle it accordingly.
Parameter description
- context.Context, events.EventResponse, and error parameter descriptions are the same as those for handler functions processing regular HTTP requests.
- The following is an example of handling a payload of type interface{}.
initializer function
The initializer is the entry point for the function's initialization logic. For instance-level global initialization within a single function, we recommend using the initializer function.
Function signature
The initializer function signatures currently supported by veFaaS are shown below. You can indicate initialization failure by returning an error (recommended) or by triggering a panic.
Parameter description
context.Context and error descriptions are the same as those for handler functions processing regular HTTP requests.
Use environment variables
For veFaaS functions, you can configure the environment variables required by the runtime of your function in the console and retrieve them in your code to implement function processing logic. veFaaS injects configured environment variables into the container where the function runs, and the program can read them using a built-in library for the programming language.
For example, if the key of an environment variable is envKey, you can use the following code sample to read the environment variable.