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

Serve resources from API routes ending in / #926

Merged
merged 4 commits into from
Aug 4, 2023
Merged
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
29 changes: 28 additions & 1 deletion chromadb/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ def _uuid(uuid_str: str) -> UUID:
raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID")


class ChromaAPIRouter(fastapi.APIRouter):
# A simple subclass of fastapi's APIRouter which treats URLs with a trailing "/" the
# same as URLs without. Docs will only contain URLs without trailing "/"s.
def add_api_route(self, path: str, *args: Any, **kwargs: Any) -> None:
# If kwargs["include_in_schema"] isn't passed OR is True, we should only
HammadB marked this conversation as resolved.
Show resolved Hide resolved
# include the non-"/" path. If kwargs["include_in_schema"] is False, include
# neither.
exclude_from_schema = (
"include_in_schema" in kwargs and not kwargs["include_in_schema"]
)

def include_in_schema(path: str) -> bool:
nonlocal exclude_from_schema
return not exclude_from_schema and not path.endswith("/")

kwargs["include_in_schema"] = include_in_schema(path)
super().add_api_route(path, *args, **kwargs)

if path.endswith("/"):
path = path[:-1]
else:
path = path + "/"

kwargs["include_in_schema"] = include_in_schema(path)
super().add_api_route(path, *args, **kwargs)


class FastAPI(chromadb.server.Server):
def __init__(self, settings: Settings):
super().__init__(settings)
Expand All @@ -84,7 +111,7 @@ def __init__(self, settings: Settings):
allow_methods=["*"],
)

self.router = fastapi.APIRouter()
self.router = ChromaAPIRouter()

self.router.add_api_route("/api/v1", self.root, methods=["GET"])
self.router.add_api_route("/api/v1/reset", self.reset, methods=["POST"])
Expand Down