-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use Depends for settings and session_local (#59)
- Loading branch information
Showing
20 changed files
with
205 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
from celery import Celery | ||
|
||
from app.shared.settings import settings | ||
|
||
|
||
def get_celery_binding() -> Celery: | ||
celery = Celery( | ||
broker_url=settings.BROKER_URL, | ||
def get_celery_binding(broker_url: str) -> Celery: | ||
return Celery( | ||
broker_url=broker_url, | ||
broker_connection_retry=False, | ||
broker_connection_retry_on_startup=False, | ||
) | ||
|
||
return celery |
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,26 +1,21 @@ | ||
from typing import Any, Generator | ||
from typing import Any | ||
|
||
from sqlalchemy import create_engine, event | ||
from sqlalchemy.orm import Session, sessionmaker | ||
from sqlalchemy import Engine, create_engine, event | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from app.shared.settings import settings | ||
|
||
engine = create_engine(settings.DATABASE_URI, connect_args={"check_same_thread": False}) | ||
def make_engine(database_url: str): | ||
engine = create_engine(database_url, connect_args={"check_same_thread": False}) | ||
|
||
@event.listens_for(engine, "connect") | ||
def set_sqlite_pragma(conn: Any, _: Any) -> None: | ||
cursor = conn.cursor() | ||
cursor.execute("PRAGMA journal_mode=WAL") | ||
cursor.close() | ||
|
||
@event.listens_for(engine, "connect") | ||
def set_sqlite_pragma(conn: Any, _: Any) -> None: | ||
cursor = conn.cursor() | ||
cursor.execute("PRAGMA journal_mode=WAL") | ||
cursor.close() | ||
return engine | ||
|
||
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
|
||
def get_session() -> Generator[Session, None, None]: | ||
session: Session = SessionLocal() | ||
try: | ||
yield session | ||
finally: | ||
session.close() | ||
def make_session_local(engine: Engine): | ||
session_local = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
return session_local |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import logging | ||
|
||
logging.basicConfig() | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
logger.setLevel(logging.INFO) |
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
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,4 +1,3 @@ | ||
from app.shared.db.base import get_session | ||
from app.web.main import app_factory | ||
|
||
app = app_factory(get_session) | ||
app = app_factory |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from functools import lru_cache | ||
from typing import Generator | ||
|
||
from fastapi import Depends | ||
from sqlalchemy.orm import Session | ||
|
||
from app.shared.db.base import make_engine, make_session_local | ||
from app.shared.settings import Settings | ||
from app.web.injections.settings import get_settings | ||
|
||
|
||
@lru_cache | ||
def session_local(database_url: str): | ||
engine = make_engine(database_url) | ||
return make_session_local(engine) | ||
|
||
|
||
def get_session_local(settings: Settings = Depends(get_settings)): | ||
return session_local(settings.DATABASE_URI) | ||
|
||
|
||
def get_session( | ||
session_local=Depends(get_session_local), | ||
) -> Generator[Session, None, None]: | ||
with session_local() as session: | ||
yield session |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from hmac import compare_digest | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, HTTPException | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
|
||
from app.shared.settings import Settings | ||
from app.web.injections.settings import get_settings | ||
|
||
|
||
def api_key_auth( | ||
credentials: Annotated[ | ||
HTTPAuthorizationCredentials, Depends(HTTPBearer(auto_error=False)) | ||
], | ||
settings: Annotated[Settings, Depends(get_settings)], | ||
): | ||
validate_credentials(credentials, settings.API_SECRET) | ||
|
||
|
||
def sharing_auth( | ||
credentials: Annotated[ | ||
HTTPAuthorizationCredentials, Depends(HTTPBearer(auto_error=False)) | ||
], | ||
settings: Annotated[Settings, Depends(get_settings)], | ||
): | ||
if settings.ENABLE_SHARING: | ||
pass | ||
else: | ||
validate_credentials(credentials, settings.API_SECRET) | ||
|
||
|
||
def validate_credentials(credentials: HTTPAuthorizationCredentials, secret: str): | ||
# use compare_digest to counter timing attacks. | ||
if ( | ||
not credentials | ||
or not secret | ||
or not compare_digest(secret, credentials.credentials) | ||
): | ||
raise HTTPException(status_code=401) |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from functools import lru_cache | ||
|
||
from app.shared.settings import Settings | ||
|
||
|
||
@lru_cache | ||
def get_settings(): | ||
return Settings() # type: ignore |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from functools import lru_cache | ||
|
||
from fastapi import Depends | ||
|
||
from app.shared.settings import Settings | ||
from app.web.injections.settings import get_settings | ||
from app.web.task_queue import TaskQueue | ||
|
||
|
||
@lru_cache | ||
def task_queue(broker_url: str): | ||
return TaskQueue(broker_url) | ||
|
||
|
||
def get_task_queue(settings: Settings = Depends(get_settings)): | ||
return task_queue(settings.BROKER_URL) |
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
Oops, something went wrong.