Skip to content

Commit

Permalink
Update General Config & Docker Setup (#31)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dhruv-ahuja authored Sep 6, 2024
1 parent d79fd83 commit 9f2b647
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
32 changes: 17 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -59,7 +61,7 @@ services:
- backend_burger_network
depends_on:
- redis_burger
- mongodb_burger
# - mongodb_burger

networks:
backend_burger_network:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/config/constants/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@
"<": "$lt",
"<=": "$lte",
}

CORS_ALLOWED_HOSTS = ["http://localhost:5173", "http://127.0.0.1:5173", "http://0.0.0.0:5173"]
2 changes: 2 additions & 0 deletions src/config/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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("/")
Expand Down
26 changes: 0 additions & 26 deletions src/schemas/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/utils/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9f2b647

Please sign in to comment.