-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |