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

Running behind a proxy server on sub-path #223

Closed
jaysnm opened this issue Aug 11, 2021 · 1 comment
Closed

Running behind a proxy server on sub-path #223

jaysnm opened this issue Aug 11, 2021 · 1 comment

Comments

@jaysnm
Copy link
Contributor

jaysnm commented Aug 11, 2021

Running the app behind proxy-server - see my work-around - works fine except that links on /docs remained pointing to / instead of /stac path prefix.

image

@jaysnm
Copy link
Contributor Author

jaysnm commented Dec 4, 2021

To let the maintainer team focus on more pressing issues, I'm going to close this issue. I was not able to fix sub-path links on /docs although the API fully works with proper routing to sub-path. This is how to do it using StacAPI app


"""Define application settings.
settings.py
"""

import pydantic


class ApiSettings(pydantic.BaseSettings):
    """FASTAPI application settings."""

    name: str = "stac-fastapi"
    description: str = "stac-fastapi"
    cors_origins: str = "*"
    root_path: str = ""

    @pydantic.validator("cors_origins")
    def parse_cors_origin(cls, v):
        """Parse CORS origins."""
        return [origin.strip() for origin in v.split(",")]

    class Config:
        """model config"""

        env_file = ".env"
        env_prefix = "STAC_API_"


"""STAC API application module - SqlAlchemy backend.
main.py
"""
from stac_fastapi.api.app import StacApi
from stac_fastapi.api.version import __version__ as stac_version
from stac_fastapi.extensions.core import (
    FieldsExtension,
    QueryExtension,
    SortExtension,
    TransactionExtension,
    ContextExtension,
)
from stac_fastapi.extensions.third_party import BulkTransactionExtension
from stac_fastapi.sqlalchemy.config import SqlalchemySettings
from stac_fastapi.sqlalchemy.core import CoreCrudClient
from stac_fastapi.sqlalchemy.session import Session
from stac_fastapi.sqlalchemy.transactions import (
    BulkTransactionsClient,
    TransactionsClient,
)
from stac_fastapi.sqlalchemy.types.search import SQLAlchemySTACSearch

from settings import ApiSettings
from fastapi.openapi.utils import get_openapi

api_settings = ApiSettings()

settings = SqlalchemySettings()
session = Session.create_from_settings(settings)
api = StacApi(
    settings=settings,
    extensions=[
        TransactionExtension(
            client=TransactionsClient(session=session), settings=settings
        ),
        BulkTransactionExtension(client=BulkTransactionsClient(session=session)),
        FieldsExtension(),
        QueryExtension(),
        SortExtension(),
        ContextExtension(),
    ],
    client=CoreCrudClient(session=session),
    search_request_model=SQLAlchemySTACSearch,
)

api.client.stac_version = stac_version
api.client.title = api_settings.name
api.client.description = api_settings.description

# app from api
app = api.app
app.root_path = api_settings.root_path

def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title=api_settings.name, 
        version=stac_version,
        routes=app.routes,
        description=api_settings.description
    )
    return openapi_schema

app.openapi = custom_openapi

# other app customizations

Use your favorite ASGI server to run the app
uvicorn main:app

This example is an inspiration from discussions on #211 and #207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant