-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve use dependency injection
- remove settings global - remove engine / session globals
- Loading branch information
Showing
16 changed files
with
176 additions
and
110 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 get_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 get_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
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,6 @@ | ||
from app.shared.db.base import get_session | ||
from app.shared.settings import Settings | ||
from app.web.main import app_factory | ||
|
||
app = app_factory(get_session) | ||
|
||
def app(): | ||
return app_factory(Settings()) # type: ignore |
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,12 @@ | ||
from typing import Generator | ||
|
||
from fastapi import Request | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
def get_session(request: Request) -> Generator[Session, None, None]: | ||
session: Session = request.app.state.session_local() | ||
try: | ||
yield session | ||
finally: | ||
session.close() |
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 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) | ||
|
||
|
||
def authenticate_api_key( | ||
credentials: Annotated[ | ||
HTTPAuthorizationCredentials, Depends(HTTPBearer(auto_error=False)) | ||
], | ||
settings: Annotated[Settings, Depends(get_settings)], | ||
): | ||
validate_credentials(credentials, settings.API_SECRET) | ||
|
||
|
||
def authenticate_sharing( | ||
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) |
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,5 @@ | ||
from fastapi import Request | ||
|
||
|
||
def get_settings(request: Request): | ||
return request.app.state.settings |
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,5 @@ | ||
from fastapi import Request | ||
|
||
|
||
def get_task_queue(request: Request): | ||
return request.app.state.task_queue |
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
Oops, something went wrong.