Skip to content

Commit

Permalink
#159 - Enable Connection to ENP DB (#168)
Browse files Browse the repository at this point in the history
Co-authored-by: Corey Carvalho <[email protected]>
  • Loading branch information
EvanParish and coreycarvalho authored Feb 25, 2025
1 parent ab3115f commit b801ac2
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 42 deletions.
13 changes: 7 additions & 6 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
export ACCESS_TOKEN_EXPIRE_SECONDS=60
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export ENP_DB_NAME=va-enp-api-db
export NAPI_DB_READ_URI=postgresql+psycopg://postgres:LocalPassword@localhost:5432/notification_api
export NAPI_DB_WRITE_URI=postgresql+psycopg://postgres:LocalPassword@localhost:5432/notification_api
export ENP_DB_READ_URI=postgresql+psycopg://postgres:LocalPassword@localhost:5433/va-enp-api-db
export ENP_DB_WRITE_URI=postgresql+psycopg://postgres:LocalPassword@localhost:5433/va-enp-api-db
export DB_HOSTNAME=localhost
export DB_HOSTNAME_READ=localhost
export DB_NAME=va_enp_api
export ENP_ADMIN_SECRET_KEY=not-very-secret
export ENV=local
export ENP_DB_PORT=5433
export ENV=local
export NAPI_DB_READ_URI=postgresql+psycopg://postgres:LocalPassword@localhost:5432/notification_api
export NAPI_DB_WRITE_URI=postgresql+psycopg://postgres:LocalPassword@localhost:5432/notification_api
16 changes: 11 additions & 5 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ fileignoreconfig:
- filename: .env.example
checksum: f04bd8b8a51131d412bea4b8098e714ca01f4c7475eeb0a40d196d922070ba43
- filename: .env.local
checksum: e64221548efb0246db08e35ce2122157d9a7e985f748367f42c9171f172884c0
checksum: 60e7f10e36a8635b33bc26945e6336ae183fe603a15169e462027339360a00f4
- filename: app/auth.py
checksum: 06eb4b4c65191921104dc39381a937843a6ce155eeac7dc72990b5d10b5eb275
- filename: app/db/__init__.py
checksum: 7373a948a440a47b385309ba07ec140a74c87fcfb6ce933544b0ccc8932da455
checksum: 66575249d84eff02f539737ad2fa9f5a59b1b8982b5a3b8ff114e309ef62303d
- filename: app/db/models.py
checksum: 4ef96985d2353c00b229b30078f150ac80b15bd804b2d31c34784671793fec42
- filename: app/providers/provider_aws.py
checksum: f345ea8dea997d72c8d40a0cac2a2b1aacacef8959088ba5cc2ea9e5df57c9fd
- filename: cd/dev.env
checksum: 4c582cfb6a6b0a3dd9274c0fc8c7bfecc0b8c36eb46f1f09ddc21e66e6c786d1
- filename: cd/perf.env
checksum: 5c50afeea60016a131f94dacd3086db9cbb4c6af573be19c7de91bbb51e04db7
- filename: ci/.env.docker
checksum: 890be824556699720207a1465a09336cc1b48a4227152523401f7ba4f1d393b0
checksum: 89c9897e4d29e36b9c001d98a0c0cd28b08858d367771b5e1ec50e398bc19e23
- filename: ci/.env.local
checksum: cd9c91e000ab1e53c5a89e9e2b316d7c12e7655843ce5af4b361b5edb328774c
checksum: b9a695faea7f7f98ad3b1f221938144cc70667f70a8ab9e3828499fe1005d556
- filename: ci/.local.env
checksum: 47e9f19fdfb3655ceb51cfee25242e87d45bb05618a1cca6ac2bc8a709e0cdf9
- filename: ci/docker-compose-local.yml
checksum: c73beda98c39232441d86f6d6f6f2858274f9703f8a462eac1effacfbb9aa39d
- filename: poetry.lock
checksum: 0d37167f4fd23e1ace7119c3e7b6f3703785876d349897b3f56aed64eeedf237
checksum: ff9d7c27a7784c0c340db4d302245ef9a951b62b9998a36d8408d49cdb0ee227
- filename: postman/enp-api.postman_collection.json
checksum: 06c086251fe022a3f42f4f88d316e2034044a53e6a6ce4f32bc4c56395f7485b
- filename: scripts/run_tests.sh
checksum: 80cd986074c12205493156f86b43e732777e2fae75683780006fb1baea90b82b
- filename: tests/app/providers/__init__.py
Expand Down
33 changes: 24 additions & 9 deletions app/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import json
import os

ENP_DB_NAME = os.getenv('DB_NAME', 'va-enp-api-db')
ENP_DB_READ_URI = os.getenv(
'ENP_DB_READ_URI', 'postgresql+psycopg://postgres:LocalPassword@localhost:5433/va-enp-api-db'
)
ENP_DB_WRITE_URI = os.getenv(
'ENP_DB_WRITE_URI', 'postgresql+psycopg://postgres:LocalPassword@localhost:5433/va-enp-api-db'
)
DB_ENGINE = 'postgresql+psycopg'

ENP_DB_NAME = os.getenv('ENP_DB_NAME', 'va_enp_api')
ENP_DB_PORT = os.getenv('ENP_DB_PORT', '5433')

# These configs require a change in infra to be updated
DB_AUTH = json.loads(os.getenv('DB_AUTH', '{"username": "postgres", "password": "LocalPassword"}'))
DB_USERNAME = DB_AUTH['username']
DB_PASSWORD = DB_AUTH['password']

ENP_DB_HOSTNAME = os.getenv('DB_HOSTNAME', 'localhost')
ENP_DB_HOSTNAME_READ = os.getenv('DB_HOSTNAME_READ', 'localhost')

# get the notification api database URI
NAPI_DB_READ_URI = os.getenv(
'NAPI_DB_READ_URI', 'postgresql+psycopg://postgres:LocalPassword@localhost:5432/notification_api'
'NAPI_DB_READ_URI', f'{DB_ENGINE}://postgres:LocalPassword@localhost:5432/notification_api'
)
NAPI_DB_WRITE_URI = os.getenv(
'NAPI_DB_WRITE_URI', 'postgresql+psycopg://postgres:LocalPassword@localhost:5432/notification_api'
'NAPI_DB_WRITE_URI', f'{DB_ENGINE}://postgres:LocalPassword@localhost:5432/notification_api'
)

# Ensure the correct DB_ENGINE is used. This is necessary when deployed.
NAPI_DB_READ_URI = DB_ENGINE + '://' + NAPI_DB_READ_URI.split('://')[1]
NAPI_DB_WRITE_URI = DB_ENGINE + '://' + NAPI_DB_WRITE_URI.split('://')[1]

# Construct the ENP database URIs
ENP_DB_READ_URI = f'{DB_ENGINE}://{DB_USERNAME}:{DB_PASSWORD}@{ENP_DB_HOSTNAME_READ}:{ENP_DB_PORT}/{ENP_DB_NAME}'
ENP_DB_WRITE_URI = f'{DB_ENGINE}://{DB_USERNAME}:{DB_PASSWORD}@{ENP_DB_HOSTNAME}:{ENP_DB_PORT}/{ENP_DB_NAME}'
2 changes: 2 additions & 0 deletions app/db/db_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ async def init_db() -> None:
# notification_api database connections
await init_napi_metadata()

logger.info('...database engines initialized.')


async def create_write_engine() -> None:
"""Create the async write engine."""
Expand Down
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def simple_route() -> dict[str, str]:
return {'Hello': 'World'}


@app.post('/db/test', status_code=status.HTTP_201_CREATED, dependencies=[Depends(JWTBearer())])
@app.post('/enp/db/test', status_code=status.HTTP_201_CREATED, dependencies=[Depends(JWTBearer())])
async def db_create_test(
*,
data: str = 'hello',
Expand Down Expand Up @@ -220,7 +220,7 @@ async def fetch_notifications(session: AsyncSession, year_list: List[int]) -> Li
return notifications


@app.get('/db/test', status_code=status.HTTP_200_OK, dependencies=[Depends(JWTBearer())])
@app.get('/enp/db/test', status_code=status.HTTP_200_OK, dependencies=[Depends(JWTBearer())])
async def db_read_test(
db_session: Annotated[async_scoped_session[AsyncSession], Depends(get_read_session_with_depends)],
years: str | None = Query(
Expand Down
3 changes: 2 additions & 1 deletion cd/dev.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ENP_ALGORITHM=HS256
ENP_ACCESS_TOKEN_EXPIRE_SECONDS=60
ENP_DB_NAME=va_enp_api
ENP_DB_PORT=5433
MAX_RETRIES=2886
DB_NAME=va_enp_api
3 changes: 2 additions & 1 deletion cd/perf.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ENP_ALGORITHM=HS256
ENP_ACCESS_TOKEN_EXPIRE_SECONDS=60
ENP_DB_NAME=va_enp_api
ENP_DB_PORT=5433
MAX_RETRIES=2886
DB_NAME=va_enp_api
4 changes: 0 additions & 4 deletions cd/va-enp-api-task-definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
{
"name": "DD_SERVICE",
"value": "va-enp-api"
},
{
"name": "FLASK_APP",
"value": "application.py"
}
],
"secrets": ${TASK_DEFINITION_SECRETS_JSON},
Expand Down
13 changes: 7 additions & 6 deletions ci/.env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
AWS_ACCESS_KEY_ID=test
AWS_REGION_NAME=us-east-1
AWS_SECRET_ACCESS_KEY=test
ENP_DB_NAME=va-enp-api-db
NAPI_DB_READ_URI=postgresql+psycopg://postgres:[email protected]:5432/notification_api
NAPI_DB_WRITE_URI=postgresql+psycopg://postgres:[email protected]:5432/notification_api
ENP_DB_READ_URI=postgresql+psycopg://postgres:LocalPassword@enp-db:5432/va-enp-api-db
ENP_DB_WRITE_URI=postgresql+psycopg://postgres:LocalPassword@enp-db:5432/va-enp-api-db
DB_HOSTNAME=enp-db
DB_HOSTNAME_READ=enp-db
ENP_ADMIN_SECRET_KEY=not-very-secret
ENV=local
ENP_DB_NAME=va_enp_api
ENP_DB_PORT=5432
ENV=local
NAPI_DB_READ_URI=postgresql://postgres:[email protected]:5432/notification_api
NAPI_DB_WRITE_URI=postgresql://postgres:[email protected]:5432/notification_api
9 changes: 5 additions & 4 deletions ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ ENV PYTHONDONTWRITEBYTECODE=1 \

WORKDIR /app

# need bash for running scripts
RUN apk add --no-cache bash

# add user and give ownership of app dir
RUN adduser -h /app -D vanotify

# From context and only what is necessary, importing relative to WORKDIR
COPY app/ app

# necessary files for mkdocs
COPY scripts/create_mkdoc_files.py /app/scripts/create_mkdoc_files.py
COPY scripts/* /app/scripts/
COPY mkdocs.yml /app/mkdocs.yml

# Tests, using glob pattern to avoid copying
Expand All @@ -51,8 +54,6 @@ COPY $TEST_FOLDER/ tests
# Copy builder files - venv
COPY --from=builder /app/.venv /app/.venv

RUN echo "$POETRY_ARGS"

# these need to run only when POETRY_ARGS contain the proper setting
RUN if echo "$POETRY_ARGS" | grep -q "mkdocs"; then \
poetry run python scripts/create_mkdoc_files.py \
Expand All @@ -63,4 +64,4 @@ RUN if echo "$POETRY_ARGS" | grep -q "mkdocs"; then \
USER vanotify

# CMD here for running when deployed to infra
CMD ["gunicorn", "app.main:app", "-b", "0.0.0.0:8000", "-k", "uvicorn_worker.UvicornWorker", "-w", "4"]
CMD ["gunicorn", "app.main:app", "-b", "0.0.0.0:6012", "-k", "uvicorn_worker.UvicornWorker", "-w", "4"]
8 changes: 5 additions & 3 deletions ci/docker-compose-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ services:
POETRY_ARGS: --with static_tools,test,mkdocs
TEST_FOLDER: tests
container_name: enp-api
depends_on:
- enp-db
ports:
- 8000:8000
entrypoint: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
- 6012:6012
entrypoint: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "6012", "--reload"]
env_file:
- .env.docker
volumes:
Expand All @@ -24,4 +26,4 @@ services:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: LocalPassword
POSTGRES_DB: va-enp-api-db
POSTGRES_DB: va_enp_api
Loading

0 comments on commit b801ac2

Please sign in to comment.