Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Serve UI from root #720

Merged
merged 22 commits into from
Jul 12, 2022
Merged

Serve UI from root #720

merged 22 commits into from
Jul 12, 2022

Conversation

TheAndrewJackson
Copy link
Contributor

@TheAndrewJackson TheAndrewJackson commented Jun 27, 2022

Purpose

Update webserver to serve the admin-ui from the root (/) instead of /static/index.html

Changes

  • Add new / and /{catchall:path} routes that handle serving all of the UI files
  • Remove BASE_ASSET_URN constant from the frontend because it's no longer needed
  • Remove /static mounted directory

Checklist

  • Update CHANGELOG.md file
    • Merge in main so the most recent CHANGELOG.md file is being appended to
    • Add description within the Unreleased section in an appropriate category. Add a new category from the list at the top of the file if the needed one isn't already there.
    • Add a link to this PR at the end of the description with the PR number as the text. example: #1
  • Applicable documentation updated (guides, quickstart, postman collections, tutorial, fidesdemo, database diagram.
  • If docs updated (select one):
    • documentation complete, or draft/outline provided (tag docs-team to complete/review on this branch)
    • documentation issue created (tag docs-team to complete issue separately)
  • Good unit test/integration test coverage
  • This PR contains a DB migration. If checked, the reviewer should confirm with the author that the down_revision correctly references the previous migration before merging
  • The Run Unsafe PR Checks label has been applied, and checks have passed, if this PR touches any external services

Ticket

Fixes #666

@TheAndrewJackson TheAndrewJackson marked this pull request as ready for review June 28, 2022 11:59

@app.on_event("startup")
async def create_webapp_dir_if_not_exists() -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

This function may still be useful to have. It's mostly for the case of someone is starting up the server for the first time and they've never built the frontend before, so don't have build/static/index.html on their local filesystem. on the fidesctl side, I added some lines to the Dockerfile to create these files, but even that doesn't work all the time since we frequently mount our own local directory over the built image.

it's always annoying to test this, but these are the two things I double checked for while working on this on the fidesctl side:

  • delete your local build/static directory, then navigate to localhost:8080 and make sure it doesn't error out (I think without these lines it would, but am not positive, maybe it's more graceful than that!)
  • build and run the docker image without volume mounting to make sure that is getting the most up to date static files properly hosted at localhost:8080

if those two are working 💯 then this is probably good to go!

Copy link
Contributor

Choose a reason for hiding this comment

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

confirmed deleting build/static and navigating to localhost:8080 causes a 500 error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good find. I'll add some code that handles that situation now

Comment on lines +7 to +35
class APIRouter(FastAPIRouter):
"""
Taken from: https://github.com/tiangolo/fastapi/issues/2060#issuecomment-834868906
"""

def api_route(
self, path: str, *, include_in_schema: bool = True, **kwargs: Any
) -> Callable[[DecoratedCallable], DecoratedCallable]:
"""
Updated api_route function that automatically configures routes to have 2 versions.
One without a trailing slash and another with it.
"""
if path.endswith("/"):
path = path[:-1]

add_path = super().api_route(
path, include_in_schema=include_in_schema, **kwargs
)

alternate_path = path + "/"
add_alternate_path = super().api_route(
alternate_path, include_in_schema=False, **kwargs
)

def decorator(func: DecoratedCallable) -> DecoratedCallable:
add_alternate_path(func)
return add_path(func)

return decorator
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting solution, to handle trailing slashes. Main thing, we talked about a test for this one, otherwise, it's looking good

Copy link
Contributor

@seanpreston seanpreston Jul 8, 2022

Choose a reason for hiding this comment

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

Would echo what Dawn has said there. It's nice for the server to behave the same regardless of a trailing slash. Django for instance solves this with regex in the URL def where you can say something like:

    re_path(r'^api/v1/some-endpoint/?', an_endpoint)

where the last ? makes the preceding / optional as it will trigger a match on both api/v1/some-endpoint and api/v1/some-endpoint/.

Since we're effectively doing this automatically now, it would be good to also document that somewhere in the API docs.

@TheAndrewJackson
Copy link
Contributor Author

@pattisdr Everything should be good to go now. I added in unit tests to check that the new router is working and an API 404 check so API calls to non existent routes properly get a 404 instead of the admin ui index.html.

However, the recent #811 PR moved some routes into fideslib. Those routes are excluded from the new routing behavior because fideslib isn't using the custom router. We may want to move the new router into fideslib so all routes will have the new trailing / handling. This will also make it so the fidesctl api can use the same router.

I think it makes sense to get this merged now and address moving the new router into fideslib in a future PR.

Copy link
Contributor

@pattisdr pattisdr left a comment

Choose a reason for hiding this comment

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

Good work @TheAndrewJackson, since we need a slight tweak to the changelog, do you mind documenting that the endpoints work with both a trailing slash and non trailing slash per Sean's request?

CHANGELOG.md Outdated
Comment on lines 53 to 55
### Changed
* Update admin ui to be served from the root route `/` [#720](https://github.com/ethyca/fidesops/pull/720)

Copy link
Contributor

Choose a reason for hiding this comment

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

Small merging issue, move this up to "Unreleased"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The changelog and docs have been updated. I put the note about trailing slashes in the API reference section. If that's not the best place and I can move it to another section.

Copy link
Contributor

Choose a reason for hiding this comment

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

thank you Andrew, that looks fine!

@pattisdr pattisdr merged commit 14eed39 into main Jul 12, 2022
@pattisdr pattisdr deleted the 666_serve_ui_from_root branch July 12, 2022 13:56
sanders41 pushed a commit that referenced this pull request Sep 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Spike] Serve Admin UI from / instead of /static/index.html
4 participants