Merge pull request #182 from grillazz/179-reflect-latest-and-greatest-fastapi-improvements

179 reflect latest and greatest fastapi improvements
This commit is contained in:
Jakub Miazek 2024-11-11 15:11:52 +01:00 committed by GitHub
commit c7775c78e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 26 additions and 12 deletions

2
.env
View File

@ -5,7 +5,7 @@ PYTHONUNBUFFERED=1
POSTGRES_HOST=db POSTGRES_HOST=db
POSTGRES_PORT=5432 POSTGRES_PORT=5432
POSTGRES_DB=devdb POSTGRES_DB=devdb
POSTGRES_USER=user POSTGRES_USER=devdb
POSTGRES_PASSWORD=secret POSTGRES_PASSWORD=secret
# Redis # Redis

View File

@ -27,7 +27,7 @@ docker-create-db-migration: ## Create new alembic database migration aka databa
.PHONY: docker-test .PHONY: docker-test
docker-test: ## Run project tests docker-test: ## Run project tests
docker compose -f compose.yml -f test-compose.yml run --rm app pytest tests docker compose -f compose.yml -f test-compose.yml run --rm app pytest tests --durations=0
.PHONY: docker-test-snapshot .PHONY: docker-test-snapshot
docker-test-snapshot: ## Run project tests with inline snapshot docker-test-snapshot: ## Run project tests with inline snapshot

View File

@ -1,4 +1,6 @@
from fastapi import APIRouter, Depends, status, Request, HTTPException from typing import Annotated
from fastapi import APIRouter, Depends, status, Request, HTTPException, Form
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db from app.database import get_db
@ -29,7 +31,9 @@ async def create_user(
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse "/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
) )
async def get_token_for_user( async def get_token_for_user(
user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db) user: Annotated[UserLogin, Form()],
request: Request,
db_session: AsyncSession = Depends(get_db),
): ):
_user: User = await User.find(db_session, [User.email == user.email]) _user: User = await User.find(db_session, [User.email == user.email])

View File

@ -27,4 +27,8 @@ AsyncSessionFactory = async_sessionmaker(
async def get_db() -> AsyncGenerator: async def get_db() -> AsyncGenerator:
async with AsyncSessionFactory() as session: async with AsyncSessionFactory() as session:
# logger.debug(f"ASYNC Pool: {engine.pool.status()}") # logger.debug(f"ASYNC Pool: {engine.pool.status()}")
try:
yield session yield session
except Exception as e:
logger.error(f"Error getting database session: {e}")
raise

View File

@ -67,7 +67,7 @@ app.include_router(
dependencies=[Depends(AuthBearer())], dependencies=[Depends(AuthBearer())],
) )
_scheduler_data_store = SQLAlchemyDataStore(engine) _scheduler_data_store = SQLAlchemyDataStore(engine, schema="scheduler")
_scheduler_event_broker = RedisEventBroker( _scheduler_event_broker = RedisEventBroker(
client_or_url=global_settings.redis_url.unicode_string() client_or_url=global_settings.redis_url.unicode_string()
) )

View File

@ -6,6 +6,9 @@ from app.models.user import User
from fastapi import Request, HTTPException from fastapi import Request, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from app.utils.logging import AppLogger
logger = AppLogger().get_logger()
async def get_from_redis(request: Request, key: str): async def get_from_redis(request: Request, key: str):
@ -37,6 +40,7 @@ class AuthBearer(HTTPBearer):
raise HTTPException( raise HTTPException(
status_code=403, detail="Invalid token or expired token." status_code=403, detail="Invalid token or expired token."
) )
logger.info(f"Token verified: {credentials.credentials}")
return credentials.credentials return credentials.credentials

View File

@ -18,7 +18,7 @@ services:
- "8080:8080" - "8080:8080"
depends_on: depends_on:
- db - db
- redis - inmemory
db: db:
container_name: fsap_db container_name: fsap_db

View File

@ -1,11 +1,11 @@
-- DROP DATABASE IF EXISTS devdb;
-- CREATE DATABASE devdb;
\connect devdb; \connect devdb;
CREATE SCHEMA shakespeare; CREATE SCHEMA shakespeare;
CREATE SCHEMA happy_hog; CREATE SCHEMA happy_hog;
CREATE SCHEMA scheduler;
DROP DATABASE IF EXISTS testdb; DROP DATABASE IF EXISTS testdb;
CREATE DATABASE testdb; CREATE DATABASE testdb;
\connect testdb; \connect testdb;
CREATE SCHEMA shakespeare; CREATE SCHEMA shakespeare;
CREATE SCHEMA happy_hog; CREATE SCHEMA happy_hog;
CREATE SCHEMA scheduler;

View File

@ -1,5 +1,3 @@
version: '3.8'
services: services:
app: app:
environment: environment:

View File

@ -38,7 +38,11 @@ async def test_add_user(client: AsyncClient):
# TODO: parametrize test with diff urls including 404 and 401 # TODO: parametrize test with diff urls including 404 and 401
async def test_get_token(client: AsyncClient): async def test_get_token(client: AsyncClient):
payload = {"email": "joe@grillazz.com", "password": "s1lly"} payload = {"email": "joe@grillazz.com", "password": "s1lly"}
response = await client.post("/user/token", json=payload) response = await client.post(
"/user/token",
data=payload,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
assert response.status_code == status.HTTP_201_CREATED assert response.status_code == status.HTTP_201_CREATED
claimset = jwt.decode( claimset = jwt.decode(
response.json()["access_token"], options={"verify_signature": False} response.json()["access_token"], options={"verify_signature": False}