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

[ENH] Make root_path for app configurable #154

Merged
merged 3 commits into from
Jan 23, 2025
Merged

[ENH] Make root_path for app configurable #154

merged 3 commits into from
Jan 23, 2025

Conversation

alyssadai
Copy link
Contributor

@alyssadai alyssadai commented Jan 22, 2025

Changes proposed in this pull request:

  • The root path for the FastAPI app is now configurable via an optional environment variable NB_FAPI_ROOT_PATH
    • Meant for use in deployment scenarios with proxy servers that use a stripped path prefix

Checklist

This section is for the PR reviewer

  • PR has an interpretable title with a prefix ([ENH], [FIX], [REF], [TST], [CI], [MNT], [INF], [MODEL], [DOC]) (see our Contributing Guidelines for more info)
  • PR has a label for the release changelog or skip-release (to be applied by maintainers only)
  • PR links to GitHub issue with mention Closes #XXXX
  • Tests pass
  • Checks pass

For new features:

  • Tests have been added

For bug fixes:

  • There is at least one test that would fail under the original bug conditions.

Summary by Sourcery

Enhancements:

  • Allow the application to be deployed under a specific path, such as /fapi/v1.

Copy link

sourcery-ai bot commented Jan 22, 2025

Reviewer's Guide by Sourcery

This pull request introduces the ability to configure the root path for the FastAPI application, which is particularly useful when the application is behind a proxy. The root path is now configurable via the NB_FAPI_ROOT_PATH environment variable. Additionally, the interactive API documentation was updated to reflect the new root path.

Sequence diagram for API documentation access with configurable root path

sequenceDiagram
    actor User
    participant Browser
    participant API

    User->>Browser: Access API docs
    Browser->>API: GET {root_path}/docs
    Note over API: Uses configured root_path
    API->>Browser: Return Swagger UI HTML
    Note over Browser: Links use {root_path}/openapi.json
    Browser->>API: GET {root_path}/openapi.json
    API->>Browser: Return OpenAPI spec
    Browser->>User: Display API documentation
Loading

File-Level Changes

Change Details Files
The application's root path is now configurable via the NB_FAPI_ROOT_PATH environment variable.
  • Added a new ROOT_PATH namedtuple to app/api/utility.py to store the root path from the environment variable.
  • The FastAPI application is now initialized with the root_path parameter set to the value of the ROOT_PATH environment variable.
app/main.py
app/api/utility.py
The interactive API documentation now correctly reflects the configured root path.
  • The /docs and /redoc endpoints now use the request.scope to dynamically generate the correct openapi_url based on the configured root_path.
  • The root endpoint now uses the request.scope to dynamically generate the correct link to the API documentation based on the configured root_path.
app/main.py
Added tests to ensure the API documentation is accessible with the correct root path.
  • Added a new test case test_docs_work_using_defined_root_path to verify that the documentation is accessible with the correct root path.
  • The test case uses monkeypatch to set the root_path attribute of the app object.
tests/test_routing.py

Assessment against linked issues

Issue Objective Addressed Explanation
#152 Add environment variable for the root path
#152 Set the root_path parameter when initializing FastAPI app

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@alyssadai alyssadai added the pr-patch Incremental feature improvement, will increment patch version when merged (0.0.+1) label Jan 22, 2025
Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.37%. Comparing base (af5e602) to head (fde6fe9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #154      +/-   ##
==========================================
+ Coverage   97.17%   97.37%   +0.20%     
==========================================
  Files          23       23              
  Lines         707      724      +17     
==========================================
+ Hits          687      705      +18     
+ Misses         20       19       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alyssadai alyssadai marked this pull request as ready for review January 22, 2025 17:08
@surchs surchs self-requested a review January 22, 2025 19:16
Copy link
Contributor

@surchs surchs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @alyssadai, one question about the trailing slashes.

Otherwise 🧑‍🍳

app/main.py Show resolved Hide resolved
tests/test_routing.py Outdated Show resolved Hide resolved
@alyssadai alyssadai merged commit b2e1d38 into main Jan 23, 2025
9 checks passed
@alyssadai alyssadai deleted the add-root-path branch January 23, 2025 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-patch Incremental feature improvement, will increment patch version when merged (0.0.+1)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support subdirectory root paths when API is behind a proxy
2 participants