|
1 | | -# Python 'hello' template |
| 1 | +# Python HTTP Function |
2 | 2 |
|
3 | | -Welcome to your new Python function project! The boilerplate function |
4 | | -code can be found in [`func.py`](./func.py). This function will |
5 | | -simply print 'Hello, World!' if a request is received successfuly. |
| 3 | +## Introduction |
6 | 4 |
|
7 | | -## Endpoints |
| 5 | +A Python HTTP function built with ASGI protocol support. The implementation |
| 6 | +provides a simple HTTP endpoint that responds with "Hello World!" to incoming |
| 7 | +requests. |
8 | 8 |
|
9 | | -Running this function will expose three endpoints. |
| 9 | +The function is structured as a class-based implementation with proper lifecycle |
| 10 | +management, including configurable startup and shutdown hooks. |
10 | 11 |
|
11 | | - * `/` The endpoint for your function. |
12 | | - * `/health/readiness` The endpoint for a readiness health check |
13 | | - * `/health/liveness` The endpoint for a liveness health check |
| 12 | +## Recommended Deployment |
14 | 13 |
|
15 | | -The health checks can be accessed in your browser at |
16 | | -[http://localhost:8080/health/readiness]() and |
17 | | -[http://localhost:8080/health/liveness](). |
| 14 | +> [!NOTE] |
| 15 | +> We recommend using the host builder. |
| 16 | +> This feature is currently behind a flag because its not available for all the |
| 17 | +> languages yet so you will need to enable it. |
18 | 18 |
|
19 | | -You can use `func invoke` to send an HTTP request to the function endpoint. |
| 19 | +```bash |
| 20 | +# Enable the host builder |
| 21 | +export FUNC_ENABLE_HOST_BUILDER=1 |
| 22 | + |
| 23 | +# Deploy your code to cluster |
| 24 | +# Make sure to set the builder to use it |
| 25 | +func deploy --builder=host |
| 26 | + |
| 27 | +# Local development and testing |
| 28 | +func run --builder=host --container=false |
| 29 | +``` |
| 30 | + |
| 31 | +## Customization |
| 32 | + |
| 33 | +- This function uses the ASGI (Asynchronous Server Gateway Interface) 3.0 |
| 34 | +specification therefore its compatible with the signature `handle(scope, receive, send)` |
| 35 | + |
| 36 | +### Lifecycle Management |
| 37 | +The function provides lifecycle hooks: |
| 38 | +- **start()**: Initialization hook called when function instances are created |
| 39 | +- **stop()**: Cleanup hook, ensuring graceful termination and resource cleanup |
| 40 | +- **alive()**: Health check exposed at `/health/liveness` |
| 41 | +- **ready()**: Readiness check exposed at `/health/readiness` |
20 | 42 |
|
21 | 43 | ## Testing |
22 | 44 |
|
23 | | -This function project includes a [unit test](./test_func.py). Update this |
24 | | -as you add business logic to your function in order to test its behavior. |
| 45 | +The function includes unit tests that verify the HTTP handler behavior. |
| 46 | +Tests are located in the `tests/` directory and use pytest with asyncio support. |
| 47 | + |
| 48 | +To run the tests: |
25 | 49 |
|
26 | | -```console |
27 | | -python test_func.py |
| 50 | +```bash |
| 51 | +# Install dependencies (if not already installed) |
| 52 | +pip install -e . |
| 53 | + |
| 54 | +# Run tests |
| 55 | +pytest |
| 56 | + |
| 57 | +# Run tests with verbose output |
| 58 | +pytest -v |
28 | 59 | ``` |
| 60 | + |
| 61 | +### Test Structure |
| 62 | + |
| 63 | +Tests are organized in the `tests/` directory with the current test file |
| 64 | +`test_func.py` verifying that the ASGI handler returns a proper 200 OK response. |
| 65 | +The testing framework uses pytest with asyncio support, configured in `pyproject.toml`. |
| 66 | + |
| 67 | +### Writing New Tests |
| 68 | + |
| 69 | +To add new tests, create files named `test_*.py` in the `tests/` directory. |
| 70 | +For async functions, use the `@pytest.mark.asyncio` decorator. Mock ASGI |
| 71 | +components by creating mock `scope`, `receive`, and `send` functions as shown |
| 72 | +in the existing test. You can also test lifecycle methods like `start()`, |
| 73 | +`stop()`, `alive()`, and `ready()` by calling them directly on a function instance. |
0 commit comments