Skip to content

route resolution fails when titiler is mounted as a subapp #95

@geospatial-jeff

Description

@geospatial-jeff

request.url_for fails to find the correct route when titiler is mounted as a sub application (https://github.com/developmentseed/titiler/blob/nonTMSReader/titiler/endpoints/factory.py#L1013):

from fastapi import FastAPI
from titiler.endpoints.stac import STACTiler

app = FastAPI()

titiler_app = FastAPI()
titiler_app.include_router(STACTiler().router)

app.mount("/titiler", titiler_app)

This is because request.url_for uses the router of the request's current scope which in this case is app and not titiler_app. Because sub applications are independent of the parent app, the titiler routes are not actually registered on app, so starlette can't find the route and raises starlette.routing.NoMatchFound.

A solution that works is replacing calls to request.url_for with self.router.url_path_for which has the same signature (its called under the hood by request.url_for) but only searches the specific router, ensuring that route resolution will always work no matter how the app is mounted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions