Skip to content

Commit 5cd779c

Browse files
committed
refactor
1 parent e13a7c9 commit 5cd779c

File tree

9 files changed

+205
-173
lines changed

9 files changed

+205
-173
lines changed

app/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
if __name__ == "__main__":
99
granian.Granian( # type: ignore[attr-defined]
10-
target="app.application:application",
11-
address="0.0.0.0", # noqa: S104
10+
target="app.application:build_app",
11+
factory=True,
12+
address=settings.app_host,
1213
port=settings.app_port,
1314
interface=Interfaces.ASGI,
1415
log_level=LogLevels(settings.log_level),

app/application.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,3 @@ def build_app() -> litestar.Litestar:
3535
)
3636
bootstrapper = LitestarBootstrapper(bootstrap_config=bootstrap_config)
3737
return bootstrapper.bootstrap()
38-
39-
40-
application = build_app()

app/schemas.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class CardCreate(CardBase):
1717

1818

1919
class Card(CardBase):
20-
model_config = pydantic.ConfigDict(from_attributes=True)
21-
2220
id: PositiveInt
2321
deck_id: PositiveInt | None = None
2422

@@ -37,8 +35,6 @@ class DeckCreate(DeckBase):
3735

3836

3937
class Deck(DeckBase):
40-
model_config = pydantic.ConfigDict(from_attributes=True)
41-
4238
id: PositiveInt
4339
cards: list[Card] | None
4440

app/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Settings(pydantic_settings.BaseSettings):
1515
db_max_overflow: int = 0
1616
db_pool_pre_ping: bool = True
1717

18+
app_host: str = "0.0.0.0" # noqa: S104
1819
app_port: int = 8000
1920

2021
opentelemetry_endpoint: str = ""

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
- .:/code
99
- /code/.venv
1010
ports:
11-
- "8001:8000"
11+
- "8000:8000"
1212
depends_on:
1313
db:
1414
condition: service_healthy

pyproject.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
2-
name = "litestar-sqlalchemy-template"
2+
name = "fastapi-sqlalchemy-template"
33
version = "0"
4-
description = "Async template on LiteStar and SQLAlchemy 2"
4+
description = "Async template on FastAPI and SQLAlchemy 2"
55
readme = "README.md"
66
requires-python = ">=3.13"
77
authors = [
@@ -11,10 +11,10 @@ license = "MIT License"
1111
dependencies = [
1212
"litestar",
1313
"lite-bootstrap[litestar-all]",
14+
"modern-di-litestar",
1415
"advanced-alchemy",
1516
"pydantic-settings",
16-
"granian",
17-
"modern-di-litestar",
17+
"granian[uvloop]",
1818
# database
1919
"alembic",
2020
"psycopg2",
@@ -32,6 +32,7 @@ dev = [
3232
"pytest",
3333
"pytest-cov",
3434
"pytest-asyncio",
35+
"asgi_lifespan",
3536
"ruff",
3637
"mypy",
3738
"asyncpg-stubs",

tests/conftest.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
import typing
22

3+
import litestar
34
import modern_di
45
import modern_di_litestar
56
import pytest
7+
from asgi_lifespan import LifespanManager
68
from httpx import ASGITransport, AsyncClient
79
from sqlalchemy.ext.asyncio import AsyncSession
810

911
from app import ioc
10-
from app.application import application
12+
from app.application import build_app
1113

1214

1315
@pytest.fixture
14-
async def client() -> typing.AsyncIterator[AsyncClient]:
16+
async def app() -> typing.AsyncIterator[litestar.Litestar]:
17+
app_ = build_app()
18+
async with LifespanManager(app_): # type: ignore[arg-type]
19+
yield app_
20+
21+
22+
@pytest.fixture
23+
async def client(app: litestar.Litestar) -> typing.AsyncIterator[AsyncClient]:
1524
async with AsyncClient(
16-
transport=ASGITransport(app=application), # type: ignore[arg-type]
25+
transport=ASGITransport(app=app), # type: ignore[arg-type]
1726
base_url="http://test",
1827
) as client:
1928
yield client
2029

2130

2231
@pytest.fixture
23-
async def di_container() -> typing.AsyncIterator[modern_di.Container]:
24-
di_container_: typing.Final = modern_di_litestar.fetch_di_container(application)
25-
async with di_container_:
26-
yield di_container_
32+
def di_container(app: litestar.Litestar) -> modern_di.Container:
33+
return modern_di_litestar.fetch_di_container(app)
2734

2835

2936
@pytest.fixture(autouse=True)

tests/factories.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,5 @@ class CardModelFactory(SQLAlchemyFactory[models.Card]):
1414
id = None
1515

1616

17-
class DeckCreateSchemaFactory(ModelFactory[schemas.DeckCreate]):
18-
__model__ = schemas.DeckCreate
19-
20-
2117
class CardCreateSchemaFactory(ModelFactory[schemas.CardCreate]):
2218
__model__ = schemas.CardCreate

uv.lock

Lines changed: 181 additions & 148 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)