Skip to content

Commit cc1bc5e

Browse files
authored
docs(fastapi): refresh integration guide for default provider (#54)
- Document the shipped FastAPIProvider as the primary, batteries-included path - Add Default Configuration section for FastAPIConfig fields - Add config publishing via 'provider:publish --provider fastapi' - Demote the custom provider example to an advanced subsection - Fix serve command to 'uv run artisan serve'
1 parent 615ae0b commit cc1bc5e

1 file changed

Lines changed: 74 additions & 6 deletions

File tree

docs/fastapi.md

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,77 @@ app: Application = Application(
3939

4040
## The FastAPI Provider
4141

42-
The `FastAPIProvider` is responsible for initializing the `FastAPI` instance and registering it with the application container. You can customize your FastAPI instance by extending this provider.
42+
The `FastAPIProvider` shipped with Fastapi Startkit is the default, batteries-included entry point. Once registered (see [Setup](#setup)), it configures everything needed to run a FastAPI application — you do **not** need to write your own provider to get started.
4343

44-
### Customizing the Instance
44+
Across its two boot phases it:
4545

46-
To change the title, version, or add global exception handlers, you can create your own provider:
46+
- **`register()`** — loads the FastAPI configuration (merging your published `config/fastapi.py` over the framework defaults) and creates the `FastAPI` instance, binding it into the application container via `use_fastapi()`.
47+
- **`boot()`** — registers the `serve` console command, wires the framework's exception handlers (so `HTTPException`, request-validation errors, and otherwise uncaught exceptions are rendered through the exception manager), and publishes the default `config/fastapi.py` so you can export and edit it.
48+
49+
Registering `FastAPIProvider` in your providers list is all that is required — the FastAPI instance, the `serve` command, and exception handling are wired up for you.
50+
51+
## Configuration
52+
53+
### Default Configuration
54+
55+
FastAPI settings are defined by the `FastAPIConfig` dataclass, with values sourced from environment variables:
56+
57+
| Field | Environment variable | Default |
58+
|---|---|---|
59+
| `app_url` | `APP_URL` | `http://127.0.0.1:8000` |
60+
| `reload` | `APP_RELOAD` | `True` |
61+
| `reload_dirs` || `None` |
62+
| `reload_excludes` || `["*.log", "tests/*", "node_modules/*"]` |
63+
64+
- `app_url` — the host and port the `serve` command binds to.
65+
- `reload` — whether Uvicorn watches for code changes and restarts automatically.
66+
- `reload_dirs` — an optional list of directories to watch; `None` lets Uvicorn use its default.
67+
- `reload_excludes` — glob patterns Uvicorn ignores while watching for changes.
68+
69+
For most applications, setting `APP_URL` and `APP_RELOAD` in your `.env` is all you need:
70+
71+
```bash
72+
# .env
73+
APP_URL=http://127.0.0.1:8000
74+
APP_RELOAD=true
75+
```
76+
77+
### Publishing the Config
78+
79+
To customise the configuration beyond environment variables — for example to change `reload_dirs` or `reload_excludes` — export the default config file into your project with the `provider:publish` command:
80+
81+
```bash
82+
uv run artisan provider:publish --provider fastapi
83+
```
84+
85+
This copies the framework's default configuration into your project at `config/fastapi.py`, where you can edit it directly:
86+
87+
```python
88+
# config/fastapi.py
89+
import dataclasses
90+
91+
from fastapi_startkit.environment import env
92+
93+
94+
@dataclasses.dataclass
95+
class FastAPIConfig:
96+
app_url: str = dataclasses.field(default_factory=lambda: env("APP_URL", "http://127.0.0.1:8000"))
97+
reload: bool = dataclasses.field(default_factory=lambda: env("APP_RELOAD", True))
98+
reload_dirs: list | None = None
99+
reload_excludes: list = dataclasses.field(
100+
default_factory=lambda: [
101+
"*.log",
102+
"tests/*",
103+
"node_modules/*",
104+
]
105+
)
106+
```
107+
108+
The provider merges this file over the framework defaults at boot, so you only need to keep the fields you want to override.
109+
110+
## Advanced: Customizing the Provider
111+
112+
The default provider is enough for most applications. If you need full control over the `FastAPI` instance — for example to change the title and version, or to add custom middleware — you can write your own provider and register it in place of `FastAPIProvider`:
47113

48114
```python
49115
# app/providers/fastapi_provider.py
@@ -69,6 +135,8 @@ class MyFastAPIProvider(Provider):
69135
])
70136
```
71137

138+
When you supply your own provider you take over instance creation, so remember to register any commands (such as `ServeCommand`) and exception handlers you still want.
139+
72140
## Routing
73141

74142
Fastapi Startkit supports the standard FastAPI routing approach as well as a `Router` wrapper that adds a more expressive, MVC-style API on top.
@@ -288,13 +356,13 @@ class MyFastAPIProvider(Provider):
288356

289357
## Serving the Application
290358

291-
When you register the `ServeCommand` in your provider, you gain access to the `serve` CLI command:
359+
The default `FastAPIProvider` registers the `serve` CLI command for you (when you use a custom provider, register `ServeCommand` yourself). Start the server with:
292360

293361
```bash
294-
uv run python artisan serve
362+
uv run artisan serve
295363
```
296364

297-
This command uses Uvicorn to start your application with reasonable defaults and reload capabilities.
365+
This command uses Uvicorn to start your application, honouring the `app_url`, `reload`, `reload_dirs`, and `reload_excludes` values from your [FastAPI configuration](#configuration).
298366

299367
## Example Application
300368

0 commit comments

Comments
 (0)