A veFaaS web application is an HTTP server running in the Native Python Runtime. This topic outlines the specifications and guidelines you need to follow when developing an HTTP server.
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.
- The service must have a startup command, which can be configured when you create the web application in the console.
- 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.
- 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.
- Avoiding 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.
Development example
In the Native Python 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 example of developing an application using the FastAPI framework.
- run.sh (startup script):
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 run.sh code demonstrates the logic for obtaining the listening address (host) and port number (port) from the command-line parameters and the environment variables.
warning
The listening port of the service must match the one configured in the console.
Request processing
Since you implement your own HTTP server in the Native Python runtime, you can define custom request processing logic in the HTTP server code. The following code demonstrates a simple request handler interface.
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. In this example, the startup command is ./run.sh and the listening port is 8000. Use environment variables
You can configure the environment variables required by the runtime of your Native Python 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.