Skip to content

Commit

Permalink
chore: Remove the named bridge adapter in docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbalabanov committed Dec 16, 2024
1 parent 6e014ae commit 0c7cd2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
10 changes: 10 additions & 0 deletions app/db.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import sys
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Annotated

from fastapi import Depends
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import text
from sqlmodel.ext.asyncio.session import AsyncSession

from app import settings
Expand All @@ -25,3 +27,11 @@ async def get_db_session() -> AsyncIterator[AsyncSession]:


DBSession = Annotated[AsyncSession, Depends(get_db_session)]


async def verify_db_connection():
async with asynccontextmanager(get_db_session)() as session:
result = await session.exec(text("SELECT 1"))

if result.one()[0] != 1:
raise Exception("Could not verify database connection")
14 changes: 14 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import logging
from contextlib import asynccontextmanager

import fastapi_pagination
from fastapi import FastAPI

from app import settings
from app.db import verify_db_connection
from app.routers import entitlements

logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
await verify_db_connection()
yield


tags_metadata = [
{
"name": "Entitlements",
Expand All @@ -18,6 +31,7 @@
openapi_tags=tags_metadata,
root_path="/v1",
debug=settings.debug,
lifespan=lifespan,
)

fastapi_pagination.add_pagination(app)
Expand Down
12 changes: 0 additions & 12 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
networks:
docker_network:
driver: bridge

services:
app:
build:
context: .
dockerfile: dev.Dockerfile
working_dir: /app
restart: always
networks:
- docker_network
depends_on:
db:
condition: "service_healthy"
Expand All @@ -29,12 +23,8 @@ services:
POSTGRES_DB: "${FFC_OPERATIONS_POSTGRES_DB}"
POSTGRES_USER: "${FFC_OPERATIONS_POSTGRES_USER}"
POSTGRES_PASSWORD: "${FFC_OPERATIONS_POSTGRES_PASSWORD}"
POSTGRES_HOST: "${FFC_OPERATIONS_POSTGRES_HOST}"
POSTGRES_PORT: "${FFC_OPERATIONS_POSTGRES_PORT}"
ports:
- "${FFC_OPERATIONS_POSTGRES_PORT}:5432"
networks:
- docker_network
healthcheck:
test: ["CMD-SHELL", "pg_isready --dbname=$${POSTGRES_DB} --username=$${POSTRGRES_USER}"]
interval: 10s
Expand All @@ -48,8 +38,6 @@ services:
depends_on:
db:
condition: "service_healthy"
networks:
- docker_network
command: >
bash -c "
set -e
Expand Down

0 comments on commit 0c7cd2a

Please sign in to comment.