A veFaaS web application is an HTTP server running in the Native Runtime. This topic outlines the specifications and guidelines you need to follow when developing an HTTP server locally.
Service specifications
When developing an HTTP server locally, follow the specifications below:
- The service itself must be stateless
- If state persistence is required, store state remotely, for example, in Redis or MySQL. Service startup must not depend on local memory caches or on-disk data.
- For a simple startup command, you can enter it directly in the console.
- If the startup process is more complex, you can include a startup script in the project and specify this script when configuring the startup command in the console.
warning
The startup script must have the execute permission. Otherwise, a permission denied error will occur when you release the service. You can refer to the following command to grant execute permission to the script for all user groups in a Linux system.
- The service must listen on a port.
- Ensure the port your service code listens on matches the port configured when you create the web application in the console. A mismatch will cause a cold start failure during function deployment with the following error.
- In the service, you can obtain the specified listening port through the environment variable _FAAS_RUNTIME_PORT.
- Avoid background processes
- We recommend against starting background processes or threads (where the request has returned a response, but tasks continue executing asynchronously in the background). veFaaS dynamically scales backend instances based on request volume and cannot guarantee the survival of background processes or threads.
HTTP server code example
In the Native Runtime, veFaaS forwards the request Method, Path, Body, Query, and Headers to your HTTP Server. You can directly use the incoming request headers and body to implement your business logic.
The following is a complete code sample of an HTTP server.
Service listening port and startup
You can pass the port via a command-line parameter or obtain the configured listening port (default: 8000) via the environment variable _FAAS_RUNTIME_PORT. The following code demonstrates the logic for obtaining the port number from the command-line parameter and the environment variable.
warning
The listening port of the service must match the one configured in the console.
After obtaining the listening port, ensure that the started HTTP server listens on 0.0.0.0:port. Otherwise, deployment will fail. The following code demonstrates the logic for concatenating the service listening address and starting the HTTP server.
Request processing
Since you implement your own HTTP server in the Native runtime, you can define custom request processing logic in the HTTP server code. The following code demonstrates a simple request handler interface.
The following code demonstrates the routing for request processing.
Graceful service shutdown
When a function instance exits, veFaaS sends a SIGINT signal to your HTTP server. You can capture this signal and implement custom graceful shutdown logic. The following code demonstrates the process from signal capture to HTTP server shutdown.
Configure a startup command
After code development and compilation, you need to configure a startup command and a listening port when you create the web application in the console. The figure below assumes that the service binary is named http-server and that the listening port is 3000. Use environment variables
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.