Skip to content

Commit

Permalink
re-organize lifespan
Browse files Browse the repository at this point in the history
  • Loading branch information
jrycw committed Mar 4, 2024
1 parent 095dc35 commit f85e006
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 130 deletions.
4 changes: 2 additions & 2 deletions app/_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def add_users(db_client: AsyncIOClient, target_n_users=5):
while len(names) < target_n_users:
names.add(fker.name())

await db_client.query(
return await db_client.query(
"""\
delete Event;
delete User;
Expand All @@ -31,7 +31,7 @@ async def add_events(db_client: AsyncIOClient, target_n_events=5):
while len(names) < target_n_events:
names.add(fker.text(max_nb_chars=20).rstrip("."))

await db_client.query(
return await db_client.query(
"""\
with dummy_user:= (insert User {name:= "dummy user"}),
for name in array_unpack(<array<str>>$names)
Expand Down
65 changes: 0 additions & 65 deletions app/_lifespan.py

This file was deleted.

1 change: 1 addition & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Settings(BaseSettings):
backendhost: str = "localhost"
backendport: int = 8000
backendreload: bool = False
backendprefill: bool = False

tz: str = "UTC"
secret_csrf: str
Expand Down
71 changes: 69 additions & 2 deletions app/lifespan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
from functools import partial

import edgedb
import svcs
from edgedb.asyncio_client import AsyncIOClient
from fastapi import FastAPI

from ._lifespan import _lifespan
from ._fixtures import add_events, add_users
from .config import settings

lifespan = svcs.fastapi.lifespan(partial(_lifespan, prefill=True))

async def _lifespan(app: FastAPI, registry: svcs.Registry, *, prefill: bool):
# EdgeDB client
db_client = edgedb.create_async_client()

async def create_db_client():
"""only 1 db_client"""
yield db_client

async def ping_db_callable(_db_client):
return await _db_client.query("select 1;")

registry.register_factory(
AsyncIOClient,
create_db_client,
ping=ping_db_callable,
)

# Add users and events for dev
if prefill:
await add_users(db_client)
await add_events(db_client)

yield

await registry.aclose()


def make_lifespan(*, prefill: bool):
return svcs.fastapi.lifespan(partial(_lifespan, prefill=prefill))


lifespan = make_lifespan(prefill=settings.backendprefill)

################################
# Need to modify _tx_lifespan
################################
# @svcs.fastapi.lifespan
# async def _tx_lifespan(app: FastAPI, registry: svcs.Registry):
# async_client = edgedb.create_async_client()

# async def tx_setup_edgedb():
# await async_client.ensure_connected()
# async for tx in async_client.with_retry_options(
# edgedb.RetryOptions(0)
# ).transaction():
# async with tx:
# yield tx
# break

# async def tx_shutdown_edgedb():
# await async_client.aclose()

# async def ping_callable(client):
# return await client.query("select 1;")

# registry.register_factory(
# AsyncIOClient,
# tx_setup_edgedb,
# on_registry_close=tx_shutdown_edgedb,
# ping=ping_callable,
# )

# yield
57 changes: 0 additions & 57 deletions fastui_app/_lifespan.py

This file was deleted.

1 change: 1 addition & 0 deletions fastui_app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Settings(BaseSettings):
backendhost: str = "localhost"
backendport: int = 8000
backendreload: bool = False
backendprefill: bool = False

tz: str = "UTC"
secret_csrf: str
Expand Down
63 changes: 61 additions & 2 deletions fastui_app/lifespan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
import svcs
from fastapi import FastAPI

from ._lifespan import _lifespan
from .clients import (
BackendAsyncClient,
FrontendGetAsyncClient,
FrontendPostPutDeleteAsyncClient,
)
from .config import settings

lifespan = svcs.fastapi.lifespan(_lifespan)

async def _lifespan(app: FastAPI, registry: svcs.Registry):
backend_client = BackendAsyncClient(
base_url=f"{settings.backendschema}://{settings.backendhost}:{settings.backendport}"
)

async def creat_backend_client():
"""1 backend web client"""
yield backend_client

registry.register_factory(
BackendAsyncClient,
creat_backend_client,
)

front_get_client = BackendAsyncClient(
base_url=f"{settings.frontendschema}://{settings.frontendhost}:{settings.frontendport}"
)

async def create_frontend_get_client():
"""1 frontent web GET client"""
yield front_get_client

registry.register_factory(
FrontendGetAsyncClient,
create_frontend_get_client,
)

async def create_frontend_post_put_delete_client():
"""For every post/put/delete, we request 1 specialized web client"""
base_url = f"{settings.frontendschema}://{settings.frontendhost}:{settings.frontendport}"
async with FrontendPostPutDeleteAsyncClient(base_url=base_url) as client:
csrftoken = (await client.get("/")).cookies.get("csrftoken")
# extra_headers = (
# {"headers": {"x-csrftoken": csrftoken}} if csrftoken is not None else {}
# )
csrftoken_dict = {"x-csrftoken": csrftoken} if csrftoken is not None else {}
yield client, csrftoken_dict

registry.register_factory(
FrontendPostPutDeleteAsyncClient,
create_frontend_post_put_delete_client,
)

yield

await registry.aclose()


def make_lifespan():
return svcs.fastapi.lifespan(_lifespan)


lifespan = make_lifespan()
13 changes: 11 additions & 2 deletions tests/lifespan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from functools import partial

import svcs
from fastapi import FastAPI

from app._lifespan import _lifespan

t_lifespan = svcs.fastapi.lifespan(partial(_lifespan, prefill=False))
async def _lifespan(app: FastAPI, registry: svcs.Registry):
yield
await registry.aclose()


def make_lifespan():
return svcs.fastapi.lifespan(partial(_lifespan))


t_lifespan = make_lifespan()

0 comments on commit f85e006

Please sign in to comment.