From 31f4a32e74f6a8b5382599ccb20768312c906781 Mon Sep 17 00:00:00 2001 From: Mirofff Date: Sun, 28 Jul 2024 00:19:14 +0200 Subject: [PATCH 1/5] feat: add missing files to deploy step and change versioning variable --- .github/workflows/deploy.yml | 7 ++++--- settings.py | 2 +- src/entrypoints/rest/app.py | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ec957a0..05f3c66 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,7 +6,7 @@ on: branches: [ dev ] jobs: - Deploy: + function-deploy: runs-on: ubuntu-latest environment: dev steps: @@ -31,8 +31,9 @@ jobs: DB_TABLE=${{vars.VERSION}} VERSION=${{vars.VERSION}} include: | - ./src - ./packages + src/ + packages/ + settings.py function_app.py requirements.txt exclude: | diff --git a/settings.py b/settings.py index 1a2cbcc..a9010cf 100644 --- a/settings.py +++ b/settings.py @@ -19,7 +19,7 @@ class Settings(pydantic_settings.BaseSettings): ACCESS_TOKEN_EXPIRATION: int = pydantic.Field(60) # * minutes REFRESH_TOKEN_EXPIRATION: int = pydantic.Field(168) # * hours - VERSION: str = pydantic.Field() + VERSION: float = pydantic.Field() LOG_LEVEL: str = pydantic.Field(str) LOG_FORMATTER: str = pydantic.Field("{asctime} {levelname:10s} {message:50s} ({funcName} - {pathname}:{lineno})") diff --git a/src/entrypoints/rest/app.py b/src/entrypoints/rest/app.py index 8a60192..8e99abc 100644 --- a/src/entrypoints/rest/app.py +++ b/src/entrypoints/rest/app.py @@ -31,8 +31,8 @@ async def _lifespan(_: fastapi.FastAPI): _logger.info("REST APPLICATION SHUTDOWN - SUCCESS") -def new_fastapi_app(version: str) -> fastapi.FastAPI: - app = fastapi.FastAPI(lifespan=_lifespan, version=version, root_path=f"/{version}") +def new_fastapi_app(version: float) -> fastapi.FastAPI: + app = fastapi.FastAPI(lifespan=_lifespan, version=str(version), root_path=f"/api/v{int(version)}") app.include_router(tasks_router.router) app.include_router(auth_router.router) From 41ae567ee59915b518b5b791a000110398ec7156 Mon Sep 17 00:00:00 2001 From: Mirofff Date: Sun, 28 Jul 2024 00:21:05 +0200 Subject: [PATCH 2/5] cicd: add cicd branches to deploy workflow push trigger rule --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 05f3c66..058ba71 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,7 +3,7 @@ run-name: ${{ github.actor }} function deploy on: push: - branches: [ dev ] + branches: [ dev, "cicd/**" ] jobs: function-deploy: From d1a5f7044d27f7cfc3d87a2af4cb70f86065e1dd Mon Sep 17 00:00:00 2001 From: Mirofff Date: Sun, 28 Jul 2024 00:43:20 +0200 Subject: [PATCH 3/5] cicd: include semver usage for versioning endpoint --- function_app.py | 7 +++++-- requirements-dev.txt | 1 - requirements.txt | 3 ++- settings.py | 2 +- src/entrypoints/rest/app.py | 13 ++++++++++--- src/entrypoints/rest/routers/auth.py | 5 ++--- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/function_app.py b/function_app.py index b2c7d32..3ad6e22 100644 --- a/function_app.py +++ b/function_app.py @@ -1,7 +1,10 @@ import logging + +import semver + from packages import dispatcher -from src.entrypoints import rest as app_rest from settings import config, init_loggers +from src.entrypoints import rest as app_rest init_loggers(__name__) @@ -9,7 +12,7 @@ logger.info("FASTAPI APPLICATION CREATION...") -fastapi_app = app_rest.new_fastapi_app(config.VERSION) +fastapi_app = app_rest.new_fastapi_app(semver.Version.parse(config.VERSION)) logger.info("PASS ASGI APP TO FUNCTION") diff --git a/requirements-dev.txt b/requirements-dev.txt index cb6bc15..f7b310c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,6 +4,5 @@ black isort mypy -pydantic-mypy -r requirements.txt \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b72e742..289f82e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ types-aioboto3 types-aiobotocore-dynamodb asgi-lifespan pyjwt -argon2-cffi \ No newline at end of file +argon2-cffi +semver \ No newline at end of file diff --git a/settings.py b/settings.py index a9010cf..1a2cbcc 100644 --- a/settings.py +++ b/settings.py @@ -19,7 +19,7 @@ class Settings(pydantic_settings.BaseSettings): ACCESS_TOKEN_EXPIRATION: int = pydantic.Field(60) # * minutes REFRESH_TOKEN_EXPIRATION: int = pydantic.Field(168) # * hours - VERSION: float = pydantic.Field() + VERSION: str = pydantic.Field() LOG_LEVEL: str = pydantic.Field(str) LOG_FORMATTER: str = pydantic.Field("{asctime} {levelname:10s} {message:50s} ({funcName} - {pathname}:{lineno})") diff --git a/src/entrypoints/rest/app.py b/src/entrypoints/rest/app.py index 8e99abc..90b1fca 100644 --- a/src/entrypoints/rest/app.py +++ b/src/entrypoints/rest/app.py @@ -2,10 +2,11 @@ import logging import fastapi +import semver import settings -from src.entrypoints.rest.routers import tasks as tasks_router, auth as auth_router from src.database.dynamo import connection as dyno_connection +from src.entrypoints.rest.routers import auth as auth_router, tasks as tasks_router _logger = logging.getLogger(__name__) @@ -31,8 +32,14 @@ async def _lifespan(_: fastapi.FastAPI): _logger.info("REST APPLICATION SHUTDOWN - SUCCESS") -def new_fastapi_app(version: float) -> fastapi.FastAPI: - app = fastapi.FastAPI(lifespan=_lifespan, version=str(version), root_path=f"/api/v{int(version)}") +def new_fastapi_app(version: semver.Version) -> fastapi.FastAPI: + app = fastapi.FastAPI( + title="TODO Tasks Service", + description="Simple todos tasks service built with FastAPI and DynamoDB (provide by aioboto3 lib)", + lifespan=_lifespan, + version=str(version.finalize_version()), + root_path=f"/api/v{version.major}", + ) app.include_router(tasks_router.router) app.include_router(auth_router.router) diff --git a/src/entrypoints/rest/routers/auth.py b/src/entrypoints/rest/routers/auth.py index c7be55d..3e78284 100644 --- a/src/entrypoints/rest/routers/auth.py +++ b/src/entrypoints/rest/routers/auth.py @@ -13,7 +13,7 @@ router = fastapi.APIRouter(prefix="/auth", tags=["Auth"]) -@router.post("/register") +@router.post("/register", status_code=fastapi.status.HTTP_201_CREATED) async def register_user(dyno: repositories_depends.DynoDepends, user_register_dto: user_dtos.UserRegister): """ Register new user endpoint. @@ -25,7 +25,6 @@ async def register_user(dyno: repositories_depends.DynoDepends, user_register_dt if public_user is not None: try: await user_handlers.register_new_user(dyno, public_user, user_register_dto.password) - return fastapi.Response(status_code=fastapi.status.HTTP_201_CREATED) except user_exception.UserCreationException: raise fastapi.HTTPException( fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR, "Unknown error in user creation..." @@ -41,8 +40,8 @@ async def login_user(dyno: repositories_depends.DynoDepends, credentials: auth_d user = await user_handlers.login_user(dyno, credentials.username, credentials.password) if user: access, refresh = auth_handlers.generate_access_refresh(user) - return auth_dtos.AuthSuccess(access_token=access.jwt, refresh_token=refresh.jwt, expires_in=access.exp) + raise fastapi.HTTPException(fastapi.status.HTTP_401_UNAUTHORIZED, "Email or password is incorrect") From 3e61b15e8bd9724de282eb7052cc78fd812ce117 Mon Sep 17 00:00:00 2001 From: Mirofff Date: Sun, 28 Jul 2024 00:47:45 +0200 Subject: [PATCH 4/5] cicd: fix log level config and change table name --- .github/workflows/deploy.yml | 2 +- settings.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 058ba71..56f5613 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -28,7 +28,7 @@ jobs: DB_ENDPOINT=${{ secrets.DB_ENDPOINT }} DB_ACCESS_KEY=${{ secrets.DB_ACCESS_KEY }} DB_SECRET_KEY=${{ secrets.DB_SECRET_KEY }} - DB_TABLE=${{vars.VERSION}} + DB_TABLE=dev VERSION=${{vars.VERSION}} include: | src/ diff --git a/settings.py b/settings.py index 1a2cbcc..227ff25 100644 --- a/settings.py +++ b/settings.py @@ -21,7 +21,7 @@ class Settings(pydantic_settings.BaseSettings): VERSION: str = pydantic.Field() - LOG_LEVEL: str = pydantic.Field(str) + LOG_LEVEL: str = pydantic.Field("DEBUG") LOG_FORMATTER: str = pydantic.Field("{asctime} {levelname:10s} {message:50s} ({funcName} - {pathname}:{lineno})") model_config = pydantic_settings.SettingsConfigDict(case_sensitive=True) From 6ffdc074f08d1bd47604f4cf3bb07adfe49572bc Mon Sep 17 00:00:00 2001 From: Mirofff Date: Sun, 28 Jul 2024 00:51:08 +0200 Subject: [PATCH 5/5] fix: package dispatcher parameters --- packages/dispatcher/_dispatcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dispatcher/_dispatcher.py b/packages/dispatcher/_dispatcher.py index ce5debf..591de73 100644 --- a/packages/dispatcher/_dispatcher.py +++ b/packages/dispatcher/_dispatcher.py @@ -80,7 +80,7 @@ async def _invoke_app(self, api_gateway_event: dict): logger.debug(f"Output response: {asgi_app_response}") return asgi_app_response - async def __call__(self, event: dict): + async def __call__(self, event: dict, context: dict): logger.info("DISPATCHER STARTUP") if not event: