Skip to content

Commit 75b2ebd

Browse files
authored
chore: Update deps and make project refacto (#80)
- Use UV instead of poetry - Configure devcontainer - Update doc - Use lifespan instead app_event in fastapi application
1 parent 129a9f2 commit 75b2ebd

File tree

13 files changed

+810
-1376
lines changed

13 files changed

+810
-1376
lines changed

.devcontainer/devcontainer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "Fastapi Boilerplate container",
3+
"image": "mcr.microsoft.com/devcontainers/python:3.12",
4+
"postCreateCommand": "uv sync --all-groups --frozen && uv run prisma migrate dev --schema database/schema.prisma",
5+
"features": {
6+
"ghcr.io/va-h/devcontainers-features/uv": {
7+
"version": "latest"
8+
}
9+
},
10+
"containerEnv": {
11+
"UV_LINK_MODE": "copy",
12+
"TZ": "Europe/Paris"
13+
},
14+
"forwardPorts": [
15+
8000
16+
],
17+
"customizations": {
18+
"vscode": {
19+
"extensions": [
20+
"charliermarsh.ruff",
21+
"mhutchie.git-graph",
22+
"ms-azuretools.vscode-docker",
23+
"ms-python.mypy-type-checker",
24+
"samuelcolvin.jinjahtml",
25+
"tamasfe.even-better-toml",
26+
"ue.alphabetical-sorter",
27+
"yzhang.markdown-all-in-one",
28+
"esbenp.prettier-vscode"
29+
],
30+
"settings": {
31+
"python.defaultInterpreterPath": "/usr/local/bin/python"
32+
}
33+
}
34+
}
35+
}

.dockerignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
!database
77
!docker
88
!pyproject.toml
9-
!poetry.lock
10-
!main.py
9+
!uv.lock
10+
!main.py
11+
!README.md

.vscode/settings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"[python]": {
3+
"editor.formatOnSave": true,
4+
"editor.defaultFormatter": "charliermarsh.ruff",
5+
},
6+
"ruff.organizeImports": true,
7+
"ruff.importStrategy": "fromEnvironment",
8+
"editor.codeActionsOnSave": {
9+
"source.organizeImports.ruff": "explicit",
10+
"source.unusedImports": "explicit"
11+
},
12+
"python.analysis.autoSearchPaths": true,
13+
"python.analysis.extraPaths": [
14+
"${workspaceFolder}"
15+
],
16+
"python.analysis.typeCheckingMode": "basic",
17+
"python.languageServer": "Pylance",
18+
"python.analysis.autoImportCompletions": true,
19+
"files.associations": {
20+
"**/templates/**/*.html": "jinja-html"
21+
}
22+
}

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ For the frontend, it uses [Bootstrap](https://getbootstrap.com/) and [HTMX](http
99
## Requirements
1010

1111
- [Docker](https://docs.docker.com/get-docker/) and [docker-compose](https://docs.docker.com/compose/) v2 for running the project
12-
- [Python 3.10+](https://www.python.org/) and [Poetry](https://python-poetry.org/) for development
12+
- [Python 3.12+](https://www.python.org/) and [uv](https://docs.astral.sh/uv/) for development
1313

1414
## Quickstart
1515

@@ -18,27 +18,34 @@ For the frontend, it uses [Bootstrap](https://getbootstrap.com/) and [HTMX](http
1818
```bash
1919
cp docker-compose.override.yml.dist docker-compose.override.yml
2020
docker compose up
21+
22+
# Service is running on http://localhost:8000
2123
```
2224

2325
### Development
2426

27+
28+
To improve development experience, a devcontainer configuration is available. To setup a development environment, you can simply use [Vscode Devcontainer](https://youtu.be/b1RavPr_878). Once the devcontainer is up, just run the command `uv run fastapi dev ./app/main.py` to start the dev server that will be available on [localhost:8000](http://localhost:8000)
29+
30+
Else, you can follow those steps to configure the development environment on your host:
31+
32+
2533
```bash
2634
# Install dependencies
27-
poetry install
35+
uv sync --all-groups --frozen
36+
37+
# Install prisma client
38+
uv run prisma migrate dev --schema database/schema.prisma
2839

2940
# Run the project
30-
poetry run uvicorn app.main:app --reload
41+
uv run fastapi dev ./app/main.py
3142
```
3243

3344
### Project quality
3445

3546
```bash
36-
# Run tests
37-
poetry run pytest
38-
3947
# Run linters
40-
poetry run ruff . --fix
41-
poetry run black .
48+
uv run ruff check . --fix
4249
```
4350

4451
## Application structure

app/__init__.py

Whitespace-only changes.

app/main.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import asynccontextmanager
2+
13
from fastapi import FastAPI
24
from fastapi.staticfiles import StaticFiles
35

@@ -6,20 +8,19 @@
68
from database.engine import db
79

810

11+
@asynccontextmanager
12+
async def lifespan(app: FastAPI):
13+
await db.connect()
14+
yield
15+
await db.disconnect()
16+
17+
918
def init_app():
10-
app = FastAPI(debug=DEBUG)
19+
app = FastAPI(debug=DEBUG, lifespan=lifespan)
1120

1221
app.mount("/static", StaticFiles(directory="static"), name="static")
1322
app.include_router(router)
1423

15-
@app.on_event(event_type="startup")
16-
async def on_startup():
17-
await db.connect()
18-
19-
@app.on_event(event_type="shutdown")
20-
async def on_shutdown():
21-
await db.disconnect()
22-
2324
return app
2425

2526

docker-compose.override.yml.dist

Lines changed: 0 additions & 11 deletions
This file was deleted.

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
dockerfile: ./docker/dockerfile
66
restart: always
77
ports:
8-
- "8001:8000"
8+
- "8000:8000"
99
environment:
1010
DEBUG: False
1111
TZ: Europe/Paris

docker/dockerfile

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
FROM python:3.11-slim as requirements-stage
2-
WORKDIR /tmp
3-
RUN pip install poetry
4-
COPY ./pyproject.toml ./poetry.lock /tmp/
5-
RUN poetry export --output=requirements.txt --without-hashes
6-
7-
FROM python:3.11-slim
1+
FROM python:3.12-slim
82
WORKDIR /code
9-
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
10-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11-
RUN pip install asyncpg
3+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
4+
COPY ./uv.lock ./pyproject.toml ./README.md /code/
5+
RUN uv sync --frozen
126
COPY ./ /code/
137
COPY ./docker/entrypoint.sh /usr/bin/entrypoint
148
RUN chmod +x /usr/bin/entrypoint

docker/entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22

3-
prisma migrate dev --schema database/schema.prisma
4-
gunicorn app.main:app --timeout 0 --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --forwarded-allow-ips="*"
3+
uv run prisma migrate dev --schema database/schema.prisma
4+
uv run fastapi run ./app/main.py

0 commit comments

Comments
 (0)