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

add db connection, tiles endpoint #55

Merged
merged 3 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 45 additions & 0 deletions stac_api/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from fastapi import APIRouter, FastAPI
from fastapi.openapi.utils import get_openapi
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from starlette.requests import Request

from stac_api.api.extensions import FieldsExtension
from stac_api.api.extensions.extension import ApiExtension
Expand All @@ -20,6 +23,7 @@
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
from stac_pydantic import ItemCollection
from stac_pydantic.api import ConformanceClasses, LandingPage

Expand Down Expand Up @@ -162,6 +166,45 @@ 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 __post_init__(self):
"""post-init hook"""
# inject settings
Expand All @@ -185,5 +228,7 @@ def __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
19 changes: 18 additions & 1 deletion stac_api/api/extensions/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
from starlette.templating import Jinja2Templates

from stac_api.api.extensions.extension import ApiExtension
from stac_api.api.models import ItemUri
from stac_api.api.routes import create_endpoint_with_depends
from stac_api.clients.tiles.ogc import TilesClient
from stac_api.models.ogc import TileSetResource


@dataclass
class TilesExtension(ApiExtension):
"""titiler extension"""

client: TilesClient = TilesClient()

def register(self, app: FastAPI) -> None:
"""register extension with the application"""
from titiler.endpoints.stac import STACTiler
Expand All @@ -36,4 +42,15 @@ def stac_demo(request: Request):
media_type="text/html",
)

app.include_router(titiler_router)
app.include_router(titiler_router, prefix="/titiler", tags=["Titiler"])

app.add_api_route(
name="Get OGC Tiles Resource",
path="/collections/{collectionId}/items/{itemId}/tiles",
response_model=TileSetResource,
response_model_exclude_none=True,
response_model_exclude_unset=True,
methods=["GET"],
endpoint=create_endpoint_with_depends(self.client.get_item_tiles, ItemUri),
tags=["OGC Tiles"],
)
2 changes: 1 addition & 1 deletion stac_api/api/extensions/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ def register(self, app: FastAPI) -> None:
self.client.delete_collection, CollectionUri
),
)
app.include_router(router)
app.include_router(router, tags=["Transaction Extension"])