Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decouple database connection #74

Merged
merged 21 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.8-slim

# Any python libraries that require system libraries to be installed will likely
# need the following packages in order to build
RUN apt-get update && apt-get install -y build-essential git
RUN apt-get update && apt-get install -y build-essential git libgeos-dev
geospatial-jeff marked this conversation as resolved.
Show resolved Hide resolved

RUN pip install pipenv
ENV PIPENV_NOSPIN=true
Expand All @@ -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
217 changes: 117 additions & 100 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"stac-pydantic>=1.3.5",
"pydantic[dotenv]",
"titiler==0.1.0a12",
"fastapi-utils",
"pygeos==0.8.0",
]

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