From 563f50979aa1cd69e4f596204d2204111193c839 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Mon, 18 Mar 2024 11:41:51 +0530 Subject: [PATCH 1/3] updated readme --- .env.example | 3 ++- README.md | 39 +++++++++++++++++++++++++++++++++++---- requirements.txt | 1 + 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 24b7f53..6c2b988 100644 --- a/.env.example +++ b/.env.example @@ -8,4 +8,5 @@ OPENAI_API_KEY_WEDNESDAY= SECRET_KEY= REDIS_URL= SENTRY_DSN= -SLACK_WEBHOOK_URL= \ No newline at end of file +SLACK_WEBHOOK_URL= +DB_ROOT_PASSWORD= //this is applicable for .env.local file only diff --git a/README.md b/README.md index 4816f71..f7da966 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,26 @@ This repository provides a template for creating and deploying a FastAPI project ## Getting Started +### Requirements: +- Python 3.11 +- Docker +- redis +- mysql + ### 1. Initialize Local Environment -To set up your local environment, run the following script: +- To set up your local environment, run the following script: ``` ./scripts/initialize-env.sh ``` This script installs the necessary dependencies and prepares the environment for running the FastAPI application on your local machine. - +- Create a .env.local file with reference of .env.example + Run following command to inject env file + ```shell + set -a source .env.local set +a + ``` ### 2. Run the Project Start the project locally with the following command: @@ -59,7 +69,7 @@ If you are new to AWS Copilot or you want to learn more about AWS Copilot, pleas ``` docker run --name recorder-redis -p 6379:6379 -d redis:alpine ``` -or add the REDIS_URL in .env file +or add the REDIS_URL in .env.local file ### 6. Circuit breakers @@ -85,4 +95,25 @@ async def external_service_endpoint(): return {"message": result} except CircuitBreakerError: raise HTTPException(status_code=503, detail="Service temporarily unavailable") -``` \ No newline at end of file +``` + +### Celery Dependencies +- Run following command to initiallize the celery worker +```shell +celery -A app.app.celery worker -l info +``` +- Turn Up Celery Flower with + ```shell + flower --broker=${REDIS_URL}/6 --port=5555 + ``` + +### Running Application into Docker Container + +- Create a file .env.docker with reference of .env.example +- Inject Docker environment using + ```shell + set -a source .env.docker set +a +- use following command to turn on the application + ```shell + docker compose --env-file .env.docker up + ``` diff --git a/requirements.txt b/requirements.txt index a8ce72e..8d24e21 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,6 +27,7 @@ fastapi==0.95.2 fastapi-pagination flake8 flatbuffers==23.5.26 +flower==2.0.1 freezegun==1.4.0 frozenlist==1.3.3 ftfy==6.1.1 From 263bf40d9e1cceea46351961d042320ffd02e725 Mon Sep 17 00:00:00 2001 From: HIMANSHU Date: Mon, 18 Mar 2024 11:44:02 +0530 Subject: [PATCH 2/3] fMT REadme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f7da966..f07418b 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,9 @@ async def external_service_endpoint(): ### Celery Dependencies - Run following command to initiallize the celery worker -```shell -celery -A app.app.celery worker -l info -``` + ```shell + celery -A app.app.celery worker -l info + ``` - Turn Up Celery Flower with ```shell flower --broker=${REDIS_URL}/6 --port=5555 From 395269130c456c74cfc8897ab7e5d4c656f36772 Mon Sep 17 00:00:00 2001 From: Anas WS Date: Mon, 18 Mar 2024 12:20:42 +0530 Subject: [PATCH 3/3] readme updated - features and other section added --- README.md | 126 +++++++++++++++++++++++--------------- scripts/initialize-env.sh | 19 +++--- 2 files changed, 82 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index f07418b..114ad40 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,39 @@ - - # FastAPI Template - - This repository provides a template for creating and deploying a FastAPI project. Follow the steps below to set up the local environment, run the project, manage database migrations, and deploy the service on AWS ECS. -## Getting Started - -### Requirements: +## Table of Contents + +- [Features](#features) +- [Getting started](#getting-started) + +### Features + +- Python 3.11+ support +- SQLAlchemy 2.0+ support +- Asynchoronous capabilities +- Database migrations using Alembic +- Basic Authentication using JWT +- Caching using Redis +- Error reporting using Sentry +- Asynchoronous background tasks using Celery +- Test cases +- Dockerized application +- Readily available CRUD operations +- Readily available middlewares for rate limiting, request id injection etc +- Type checking using mypy +- Linting using flake8 +- Formatting using black +- Code quality analysis using SonarQube + +### Getting Started + +#### Requirements: - Python 3.11 - Docker -- redis - mysql -### 1. Initialize Local Environment +#### 1. Initialize Local Environment - To set up your local environment, run the following script: @@ -23,21 +42,14 @@ This repository provides a template for creating and deploying a FastAPI project ``` This script installs the necessary dependencies and prepares the environment for running the FastAPI application on your local machine. -- Create a .env.local file with reference of .env.example + +Create a .env.local file with reference of .env.example Run following command to inject env file ```shell set -a source .env.local set +a ``` -### 2. Run the Project -Start the project locally with the following command: - -``` -./scripts/local_server.sh -``` - -This script upgrades the database migrations using Alembic and starts the FastAPI server using Uvicorn. The server is hosted on 0.0.0.0 and port 8000, making it accessible locally. -### 3. Database Migrations +#### 2. Database Migrations Create new database migrations when you make changes to your models. Use the following command: ``` alembic revision -m 'brief description of changes' @@ -51,7 +63,46 @@ alembic upgrade head ``` This command updates the database schema to reflect the latest changes defined in the migration scripts -### 4. Deploy Service on AWS ECS +#### 3. Redis Dependency +``` +docker run --name recorder-redis -p 6379:6379 -d redis:alpine +``` +or add the REDIS_URL in .env.local file + + +#### 4. Celery Dependency +- Run following command to initiallize the celery worker + ```shell + celery -A app.app.celery worker -l info + ``` +- Turn Up Celery Flower with + ```shell + flower --broker=${REDIS_URL}/6 --port=5555 + ``` + +#### 5. Run the Project + +##### Running Application Locally + +``` +./scripts/local_server.sh +``` + +This script upgrades the database migrations using Alembic and starts the FastAPI server using Uvicorn. The server is hosted on 0.0.0.0 and port 8000, making it accessible locally. + +##### Running Application into Docker Container + +- Create a file .env.docker with reference of .env.example +- Inject Docker environment using + ```shell + set -a source .env.docker set +a +- use following command to turn on the application + ```shell + docker compose --env-file .env.docker up + ``` + + +#### 6. Deploy Service on AWS ECS To deploy the FastAPI application on AWS ECS, use the following script: ``` @@ -65,24 +116,18 @@ Note: Ensure you have AWS credentials configured and AWS Copilot installed for s #### New to AWS Copilot? If you are new to AWS Copilot or you want to learn more about AWS Copilot, please refer to [this helpful article](https://www.wednesday.is/writing-tutorials/how-to-use-copilot-to-deploy-projects-on-ecs) that guides you through the process of setting up AWS Copilot locally as well as also helps you understand how you can publish and update an application using 4 simple steps. -### 5. Redis Dependency -``` -docker run --name recorder-redis -p 6379:6379 -d redis:alpine -``` -or add the REDIS_URL in .env.local file -### 6. Circuit breakers +#### 7. Circuit breakers -## Using the Circuit Breaker for External API Calls +Using the Circuit Breaker for External API Calls Our application uses a circuit breaker pattern to enhance its resilience against failures in external services. The circuit breaker prevents the application from performing operations that are likely to fail, allowing it to continue operating with degraded functionality instead of complete failure. -## How to Use - +How to Use? - For any external service call, wrap the call with the circuit breaker. - The circuit breaker is configured to trip after a certain number of consecutive failures. Once tripped, it will prevent further calls to the external service for a defined period. -## Example +Example Here's an example of using the circuit breaker in an API route: @@ -96,24 +141,3 @@ async def external_service_endpoint(): except CircuitBreakerError: raise HTTPException(status_code=503, detail="Service temporarily unavailable") ``` - -### Celery Dependencies -- Run following command to initiallize the celery worker - ```shell - celery -A app.app.celery worker -l info - ``` -- Turn Up Celery Flower with - ```shell - flower --broker=${REDIS_URL}/6 --port=5555 - ``` - -### Running Application into Docker Container - -- Create a file .env.docker with reference of .env.example -- Inject Docker environment using - ```shell - set -a source .env.docker set +a -- use following command to turn on the application - ```shell - docker compose --env-file .env.docker up - ``` diff --git a/scripts/initialize-env.sh b/scripts/initialize-env.sh index bb5b67a..dd6cb70 100755 --- a/scripts/initialize-env.sh +++ b/scripts/initialize-env.sh @@ -7,28 +7,23 @@ echo "Initializing local python environment" # Initialize python environment python -m venv venv -echo +echo echo "Activating local python environment" # Activate python environment source ./venv/bin/activate -echo +echo echo "Local python environment activated" -echo +echo echo "Upgrading pip" -echo +echo # Upgrade pip pip install --upgrade pip -echo +echo echo "pip upgraded" -echo +echo echo "Installing project dependencies" -echo +echo # Install requirements/dependencies pip install -r requirements.txt --no-cache-dir - -pip uninstall httpcore -y -pip uninstall httpx -y -pip install httpcore -pip install httpx \ No newline at end of file