Skip to content

Commit

Permalink
decouple database connection from api layer (#74)
Browse files Browse the repository at this point in the history
* create Session object wrapping fastapi_utils

* add Session to core, try it out with /collections endpoint

* init session on app creation

* push Session lower

* remove db middlewares and event hooks

* relock

* finish updating core

* update pagination client

* remove PostgresClient from core

* remove PostgresClient from pagination, switch to mixin

* add lookup_id staticmethod

* make private method

* refactor transactions

* client tests working

* resource tests working

* lock pygeos to 0.8

* oops

* do exception handling in one place

* remove old code

* switch back to pygeos 0.9

* update except
  • Loading branch information
geospatial-jeff authored Jan 26, 2021
1 parent 5a8d953 commit 3838242
Show file tree
Hide file tree
Showing 15 changed files with 510 additions and 601 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ COPY . ./

ENV APP_HOST=0.0.0.0
ENV APP_PORT=80
ENV RELOAD=""
ENV RELOAD="true"

ENTRYPOINT ["pipenv", "run"]
CMD if [ "$RELOAD" ]; then uvicorn stac_api.app:app --host=${APP_HOST} --port=${APP_PORT} --reload ; \
Expand Down
252 changes: 135 additions & 117 deletions Pipfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"stac-pydantic>=1.3.5",
"pydantic[dotenv]",
"titiler==0.1.0a12",
"fastapi-utils",
]

extra_reqs = {
Expand Down
45 changes: 0 additions & 45 deletions stac_api/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import attr
from fastapi import APIRouter, FastAPI
from fastapi.openapi.utils import get_openapi
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from stac_pydantic import ItemCollection
from stac_pydantic.api import ConformanceClasses, LandingPage
from starlette.requests import Request

from stac_api.api.extensions import FieldsExtension
from stac_api.api.extensions.extension import ApiExtension
Expand All @@ -25,7 +22,6 @@
from stac_api.config import ApiSettings, inject_settings
from stac_api.errors import DEFAULT_STATUS_CODES, add_exception_handlers
from stac_api.models import schemas
from stac_api.utils.dependencies import READER, WRITER


@attr.s
Expand Down Expand Up @@ -164,45 +160,6 @@ async def ping():

self.app.include_router(mgmt_router, tags=["Liveliness/Readiness"])

def setup_db_connection(self):
"""setup database connection"""

@self.app.on_event("startup")
async def on_startup():
"""Create database engines and sessions on startup"""
self.app.state.ENGINE_READER = create_engine(
self.settings.reader_connection_string, echo=self.settings.debug
)
self.app.state.ENGINE_WRITER = create_engine(
self.settings.writer_connection_string, echo=self.settings.debug
)
self.app.state.DB_READER = sessionmaker(
autocommit=False, autoflush=False, bind=self.app.state.ENGINE_READER
)
self.app.state.DB_WRITER = sessionmaker(
autocommit=False, autoflush=False, bind=self.app.state.ENGINE_WRITER
)

@self.app.on_event("shutdown")
async def on_shutdown():
"""Dispose of database engines and sessions on app shutdown"""
self.app.state.ENGINE_READER.dispose()
self.app.state.ENGINE_WRITER.dispose()

@self.app.middleware("http")
async def create_db_connection(request: Request, call_next):
"""Create a new database connection for each request"""
if "titiler" in str(request.url):
return await call_next(request)
reader = request.app.state.DB_READER()
writer = request.app.state.DB_WRITER()
READER.set(reader)
WRITER.set(writer)
resp = await call_next(request)
reader.close()
writer.close()
return resp

def __attrs_post_init__(self):
"""post-init hook"""
# inject settings
Expand All @@ -226,7 +183,5 @@ def __attrs_post_init__(self):
# register exception handlers
add_exception_handlers(self.app, status_codes=self.exceptions)

self.setup_db_connection()

# customize openapi
self.app.openapi = self.customize_openapi
13 changes: 8 additions & 5 deletions stac_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
TransactionExtension,
)
from stac_api.clients.postgres.core import CoreCrudClient
from stac_api.clients.postgres.tokens import PaginationTokenClient
from stac_api.clients.postgres.session import Session
from stac_api.clients.postgres.transactions import TransactionsClient
from stac_api.clients.tiles.ogc import TilesClient
from stac_api.config import ApiSettings

settings = ApiSettings()
session = Session(settings.reader_connection_string, settings.writer_connection_string)
api = StacApi(
settings=ApiSettings(),
settings=settings,
extensions=[
TransactionExtension(client=TransactionsClient()),
TransactionExtension(client=TransactionsClient(session=session)),
FieldsExtension(),
QueryExtension(),
SortExtension(),
TilesExtension(),
TilesExtension(TilesClient(session=session)),
],
client=CoreCrudClient(pagination_client=PaginationTokenClient()),
client=CoreCrudClient(session=session),
)
app = api.app
70 changes: 0 additions & 70 deletions stac_api/clients/postgres/base.py

This file was deleted.

Loading

0 comments on commit 3838242

Please sign in to comment.