Event-Based Function Execution (Lambda) Plugin for Caddy v2.
The caddy-lambda triggers execution of a function when it is invoked. It is a terminal
plugin, i.e. the plugin writes response headers and body.
The Caddyfile config follows:
localhost {
route /api/* {
lambda {
name hello_world
runtime python
python_executable {$HOME}/path/to/venv/bin/python
entrypoint assets/scripts/api/hello_world/app/index.py
function handler
}
}
route {
respond "OK"
}
}
The assets/scripts/api/hello_world/app/index.py follows:
import json
def handler(event: dict) -> dict:
print(f"event: {event}")
response = {
"body": json.dumps({"message": "hello world!"}),
"status_code": 200,
}
return responseThe response dictionary is mandatory for a handler. he status_code and body are
mandatory fields of the response. The plugin writes status_code and body back to
the requestor.