This topic describes the structural characteristics, sample code, and FAQ of event handlers in a custom container runtime.
Background information
For a custom container function in web server mode, Function Compute forwards the common headers, request body, POST method, and the /invoke and /initialize paths to the HTTP server in the container. You can use the function signatures, such as context
and event
, of runtimes supported by Function Compute, such as the Golang runtime. You can also use the request headers and body as input parameters to define the service logic of a function. For more information, see Event handlers.
For a custom container function in non-web server mode, Function Compute adds the body to the startup environment of the container by using environment variables. You can use os.GetEnv("FC_CUSTOM_CONTAINER_EVENT")
to obtain event information and perform custom operations.
Function invocation
If a function in web server mode uses an event handler, the HTTP server needs to only implement the logic that corresponds to the /invoke
path and the POST
method.
Path | Request | Expected response |
POST |
| Response body: the return value of the function handler. The return value includes the response code and response header.
You can include the x-fc-status field in response headers to report to Function Compute whether the local function is successfully invoked.
Note We recommend that you specify the StatusCode and x-fc-status fields in the HTTP response. |
Sample code for a custom container in web server mode
In the following Node.js Express example, the POST method and /initialize are called by Function Compute when a function instance is initialized. The POST method and /invoke serve as handlers when Function Compute is called. The context
and event
parameters are obtained from req.headers
and req.body
, and the results of the function invocation are then returned as an HTTP response.
'use strict';
const express = require('express');
// Constants.
const PORT = 9000;
const HOST = '0.0.0.0';
const app = express();
// Parse the JSON-formatted request body.
app.use(express.json({type:['application/json', 'application/octet-stream']}))
// The example of the Initializer hook. You must configure the Initializer hook when you create the function.
app.post('/initialize', (req, res) => {
console.log(req.body)
res.send('Hello FunctionCompute, /initialize\n');
});
// Invoke the event function.
app.post('/invoke', (req, res) => {
console.log(req.body)
res.send('Hello FunctionCompute, event function\n');
});
var server = app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
server.timeout = 0; // Never time out.
server.keepAliveTimeout = 0; // keepalive, never timeout
Sample code for a custom container function in non-web server mode
In the following Python image example, the event-triggering content is obtained by using FC_CUSTOM_CONTAINER_EVENT
.
import os
if __name__ == '__main__':
FC_EVENT = os.environ['FC_CUSTOM_CONTAINER_EVENT']
print("Hello serverless image")
print("FC event is: " + FC_EVENT)