From 9f2b6475e7e91a14a89299d5839a82bc249004c1 Mon Sep 17 00:00:00 2001 From: Dhruv <83733638+dhruv-ahuja@users.noreply.github.com> Date: Fri, 6 Sep 2024 06:32:51 +0530 Subject: [PATCH] Update General Config & Docker Setup (#31) * chore: update docker config - disable mongodb service as we are using mongodb atlas currently - add env vars for pydantic logfire * chore: setup cors * chore: use 1 worker inside container * chore: update logfire configuration * chore: cleanup unneeded code --- Dockerfile | 2 +- docker-compose.yml | 32 +++++++++++++++++--------------- requirements.txt | 2 +- src/config/constants/app.py | 2 ++ src/config/services.py | 2 ++ src/main.py | 9 +++++++++ src/schemas/requests.py | 26 -------------------------- src/utils/jobs.py | 1 - 8 files changed, 32 insertions(+), 44 deletions(-) diff --git a/Dockerfile b/Dockerfile index 911a0f9..3a3ab2a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,4 +46,4 @@ RUN mkdir -p /home/myuser/log EXPOSE 8000 # using /dev/shm as the worker temp dir will help prevent random locks and freezes used for gunicorn heartbeat -CMD ["gunicorn", "-b", "0.0.0.0:8000", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "--worker-tmp-dir", "/dev/shm", "src.main:app"] +CMD ["gunicorn", "-b", "0.0.0.0:8000", "-w", "1", "-k", "uvicorn.workers.UvicornWorker", "--worker-tmp-dir", "/dev/shm", "src.main:app"] diff --git a/docker-compose.yml b/docker-compose.yml index 272f76b..78efc7e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,18 @@ version: "3" services: - mongodb_burger: - image: mongodb/mongodb-community-server:7.0.2-ubuntu2204 - container_name: mongodb_burger - environment: - - MONGO_INITDB_ROOT_USERNAME=${DB_USER} - - MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD} - ports: - - "27017:27017" - volumes: - - ~/docker_data/mongo:/data/db - networks: - - backend_burger_network + # mongodb_burger: + # image: mongodb/mongodb-community-server:7.0.2-ubuntu2204 + # container_name: mongodb_burger + # environment: + # - MONGO_INITDB_ROOT_USERNAME=${DB_USER} + # - MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD} + # ports: + # - "27017:27017" + # volumes: + # - ~/docker_data/mongo:/data/db + # networks: + # - backend_burger_network redis_burger: image: redis:7.2.4 @@ -24,8 +24,8 @@ services: - "--save 900 1" - "--appendonly yes" - "--requirepass ${REDIS_PASSWORD}" - ports: - - "6379:6379" + # ports: + # - "6379:6379" volumes: - ~/docker_data/redis:/data networks: @@ -51,6 +51,8 @@ services: - NEW_RELIC_APP_NAME=backend_burger_docker - ENV NEW_RELIC_LOG=/home/myuser/log/newrelic.log - REDIS_HOST=redis_burger + - APP_ENVIRONMENT=prod + - LOGFIRE_PYDANTIC_PLUGIN_RECORD=${LOGFIRE_PYDANTIC_PLUGIN_RECORD} env_file: - .env ports: @@ -59,7 +61,7 @@ services: - backend_burger_network depends_on: - redis_burger - - mongodb_burger + # - mongodb_burger networks: backend_burger_network: diff --git a/requirements.txt b/requirements.txt index fa8e2ce..0d58602 100644 --- a/requirements.txt +++ b/requirements.txt @@ -42,7 +42,7 @@ jmespath==1.0.1 joblib==1.4.2 kombu==5.3.4 lazy-model==0.2.0 -logfire==0.50.1 +logfire==0.51.0 loguru==0.7.2 markdown-it-py==3.0.0 MarkupSafe==2.1.5 diff --git a/src/config/constants/app.py b/src/config/constants/app.py index 1fe1d23..472f59c 100644 --- a/src/config/constants/app.py +++ b/src/config/constants/app.py @@ -49,3 +49,5 @@ "<": "$lt", "<=": "$lte", } + +CORS_ALLOWED_HOSTS = ["http://localhost:5173", "http://127.0.0.1:5173", "http://0.0.0.0:5173"] diff --git a/src/config/services.py b/src/config/services.py index 86c285b..6db194f 100644 --- a/src/config/services.py +++ b/src/config/services.py @@ -60,6 +60,7 @@ class Settings(BaseSettings): redis_host: str redis_password: SecretStr | None = None app_environment: str + logfire_pydantic_plugin_record: str = "metrics" def generate_settings_config(env_location: str | None = None) -> Settings: @@ -85,6 +86,7 @@ def initialize_logfire_services(app: FastAPI) -> None: logfire.instrument_fastapi(app) logfire.instrument_redis() logfire.instrument_pymongo() + logfire.instrument_system_metrics() def initialize_aws_session(key_id: str, key_secret: str, region_name: str) -> boto3.Session: diff --git a/src/main.py b/src/main.py index be57ca7..0d164f0 100644 --- a/src/main.py +++ b/src/main.py @@ -1,11 +1,13 @@ import dotenv from fastapi import FastAPI from fastapi.exceptions import RequestValidationError +from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import ORJSONResponse import newrelic.agent from pydantic import ValidationError from starlette import status +from src.config.constants.app import CORS_ALLOWED_HOSTS from src.config.exceptions import ( handle_validation_exception, handle_not_found_exception, @@ -42,6 +44,13 @@ app.add_middleware(ExceptionHandlerMiddleware) app.add_middleware(LoggingMiddleware) +app.add_middleware( + CORSMiddleware, + allow_origins=CORS_ALLOWED_HOSTS, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) @app.get("/") diff --git a/src/schemas/requests.py b/src/schemas/requests.py index 69c18cd..f1f2a3e 100644 --- a/src/schemas/requests.py +++ b/src/schemas/requests.py @@ -99,29 +99,3 @@ def parse_sort_input(query_params: list[str] | None) -> list["SortSchema"] | Non class FilterSortInput(BaseModel): filter_: Annotated[FilterInputType, BeforeValidator(FilterSchema.parse_filter_input)] = Field(None, alias="filter") sort: Annotated[SortInputType, BeforeValidator(SortSchema.parse_sort_input)] - - @staticmethod - def parse_filter_input(query_params: list[str] | None) -> list[FilterSchema] | None: - if query_params is None: - return - - filter_params = [] - valid_params = True - - for query_param in query_params: - try: - field, operation, value = query_param.split(":") - if operation not in FILTER_OPERATION.__args__: # type: ignore - valid_params = False - break - - filter_param = FilterSchema(field=field, operation=operation, value=value) # type: ignore - except Exception: - valid_params = False - - if not valid_params: - raise ValueError("Invalid input. Incorrect 'filter' query params.") - - filter_params.append(filter_param) - - return filter_params diff --git a/src/utils/jobs.py b/src/utils/jobs.py index ef5b2c3..bdca0d0 100644 --- a/src/utils/jobs.py +++ b/src/utils/jobs.py @@ -88,7 +88,6 @@ def convert_decimal(dict_item: dict | None): if dict_item is None: return None - # for k,v in list(dict_item.items()): for key, value in dict_item.items(): if isinstance(value, dict): convert_decimal(value)