Skip to content

Commit

Permalink
cicd: include semver usage for versioning endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirofff committed Jul 27, 2024
1 parent 41ae567 commit d1a5f70
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
7 changes: 5 additions & 2 deletions function_app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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__)

logger = logging.getLogger()

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")

Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ black
isort

mypy
pydantic-mypy

-r requirements.txt
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ types-aioboto3
types-aiobotocore-dynamodb
asgi-lifespan
pyjwt
argon2-cffi
argon2-cffi
semver
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})")
Expand Down
13 changes: 10 additions & 3 deletions src/entrypoints/rest/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/entrypoints/rest/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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..."
Expand All @@ -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")


Expand Down

0 comments on commit d1a5f70

Please sign in to comment.