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

fix(standalone): fix api path when embedding front-end #2136

Conversation

sylvlecl
Copy link
Member

@sylvlecl sylvlecl commented Sep 9, 2024

Context

When including the front-end application in the python server (standalone or "desktop" mode),
we want to be able to append a /api prefix to backend URLs.
So far, this has not been correctly done:
root_path has been used, whereas it does not actually change the URLs, its only purpose
is for the server to know that it's behind a proxy which exposes this prefix to the client,
see fastapi doc about this.

This has more or less worked so far because of a bug in starlette: the server indeed served
the services at both URLs: with a without the prefix.

With newer fastapi and starlette versions, this does not work anymore: the desktop application
is broken
. We need to clean up the implementation to actually serve the services with a prefix.

Implementation

Another configuration property api_prefix is introduced, which is used to prefix all
backend routes (through a "root" APIRouter).

All front related logic is moved to a dedicated module antarest.front.

sylvlecl and others added 5 commits September 9, 2024 11:26
The way we build the standalone version is completely
reviewed, because it does not work anymore with recent
versions of starlette.

The existing code was not correctly using root_path.
Actually, adding a prefix to the API requires to explicitly
add it to routes. It's a new configuration property "api_prefix"
(using fastapi sub-application could be an alternative solution).

Signed-off-by: Sylvain Leclerc <[email protected]>
Enable the use of pytest-xdist to run tests in parallel, in CI
or developer env.

Following issues with parallel execution of tests are solved :

- some checks depended on the consistency at 2 moments in time of the
result of disk_usage, which was anyway not a good idea (dependent on
what happens elsewhere on the machine). It's now replaced with mocking
- some parameterized tests had random sort order, which is not allowed
by pytest-xdist (all workers must have exactly the same tests in the
same order)
- the command_factory test checked something in its teardown, assuming
all tests were executed in the same process, which is not the case
anymore. The same check is done as a separate unit tests.

Possible remaining issue which could cause some timeouts:
some services hold FileLock which have the same identifier
accross multiple tests.

Signed-off-by: Sylvain Leclerc <[email protected]>
@mabw-rte mabw-rte force-pushed the fix/fix-standalone-mode-api-prefix branch from 431bb60 to 722629b Compare September 9, 2024 09:27
@mabw-rte mabw-rte assigned mabw-rte and sylvlecl and unassigned mabw-rte Sep 9, 2024
Copy link
Contributor

@mabw-rte mabw-rte left a comment

Choose a reason for hiding this comment

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

I like the fact that you eliminated a lot of duplicate code.

@@ -58,4 +60,5 @@ def camel_case_model(model: t.Type[BaseModel]) -> t.Type[BaseModel]:
field.alias = new_alias
field.validation_alias = new_alias
field.serialization_alias = new_alias
field.alias_priority = 2
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer defining constants like these as global variables for the module.
This enforces maintainability and gives an explicit documentation for the variable using via the naming.
Example:
DEFAULT_ALIAS_PRIORITY=2

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed! Actually I just removed the line, I left it by mistake, this change is not needed.

@sylvlecl sylvlecl merged commit 669ce11 into feature/1724-upgrade-python-dependencies Sep 11, 2024
8 of 10 checks passed
@sylvlecl sylvlecl deleted the fix/fix-standalone-mode-api-prefix branch September 11, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants