Skip to content

Serverless Function Deploy #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ run-name: ${{ github.actor }} function deploy

on:
push:
branches: [ dev ]
branches: [ dev, "cicd/**" ]

jobs:
Deploy:
function-deploy:
runs-on: ubuntu-latest
environment: dev
steps:
Expand All @@ -28,11 +28,12 @@ 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
./packages
src/
packages/
settings.py
function_app.py
requirements.txt
exclude: |
Expand Down
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
2 changes: 1 addition & 1 deletion packages/dispatcher/_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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 @@ -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)
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: str) -> fastapi.FastAPI:
app = fastapi.FastAPI(lifespan=_lifespan, version=version, root_path=f"/{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