Skip to content

Commit

Permalink
feat: OpenAPI schema + interactive docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed May 22, 2024
1 parent 29d2eeb commit 26ae9a2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
10 changes: 9 additions & 1 deletion bento_reference_service/authz.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from bento_lib.auth.middleware.fastapi import FastApiAuthMiddleware
from .config import get_config
from .logger import get_logger
Expand All @@ -8,4 +9,11 @@

# Non-standard middleware setup so that we can import the instance and use it for dependencies too
config = get_config() # TODO: Find a way to DI this
authz_middleware = FastApiAuthMiddleware.build_from_pydantic_config(config, get_logger(config))
authz_middleware = FastApiAuthMiddleware.build_from_pydantic_config(
config,
get_logger(config),
exempt_request_patterns=(
(r"GET", re.escape(config.service_docs_path)),
(r"GET", re.escape(config.service_openapi_path)),
),
)
3 changes: 3 additions & 0 deletions bento_reference_service/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Config(BentoBaseConfig):
service_description: str = "Reference data (genomes & annotations) service for the Bento platform."
service_url_base_path: str = "http://127.0.0.1:5000" # Base path to construct URIs from

service_docs_path: str = "/docs"
service_openapi_path: str = "/openapi.json"

database_uri: str = "postgres://localhost:5432"
file_ingest_tmp_dir: Path = Path(__file__).parent.parent / "tmp" # Default to repository `tmp` folder
file_ingest_chunk_size: int = 1024 * 256 # 256 KiB at a time
Expand Down
10 changes: 4 additions & 6 deletions bento_reference_service/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import re
from bento_lib.service_info.constants import SERVICE_GROUP_BENTO
from bento_lib.service_info.helpers import build_bento_service_type
from bento_reference_service import __version__

__all__ = [
Expand All @@ -11,13 +13,9 @@

BENTO_SERVICE_KIND = "reference"

SERVICE_GROUP = "ca.c3g.bento"
SERVICE_GROUP = SERVICE_GROUP_BENTO
SERVICE_ARTIFACT = BENTO_SERVICE_KIND

SERVICE_TYPE = {
"group": SERVICE_GROUP,
"artifact": SERVICE_ARTIFACT,
"version": __version__,
}
SERVICE_TYPE = build_bento_service_type(SERVICE_ARTIFACT, __version__)

RANGE_HEADER_PATTERN = re.compile(r"^bytes=(\d+)-(\d+)?$")
10 changes: 9 additions & 1 deletion bento_reference_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import RequestValidationError, StarletteHTTPException
from urllib.parse import urlparse

from bento_lib.responses.fastapi_errors import (
http_exception_handler_factory,
Expand Down Expand Up @@ -37,7 +38,14 @@ async def lifespan(_app: FastAPI):
yield


app = FastAPI(lifespan=lifespan)
app = FastAPI(
title=config_for_setup.service_name,
root_path=urlparse(config_for_setup.service_url_base_path).path,
docs_url=config_for_setup.service_docs_path,
openapi_url=config_for_setup.service_openapi_path,
version=__version__,
lifespan=lifespan,
)

# Attach different routers to the app, for:
# - genome listing
Expand Down
39 changes: 20 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
python = "^3.10.0"
fastapi = "^0.111.0"
pydantic = "^2.6.1"
bento-lib = {extras = ["fastapi"], version = "^11.7.2"}
bento-lib = {extras = ["fastapi"], version = "^11.9.0"}
aiofiles = "^23.2.1"
pysam = "~0.22.0"
jsonschema = "^4.21.1"
Expand Down

0 comments on commit 26ae9a2

Please sign in to comment.