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

Mount problem #443

Closed
15864620573 opened this issue Dec 5, 2023 · 2 comments · Fixed by #444
Closed

Mount problem #443

15864620573 opened this issue Dec 5, 2023 · 2 comments · Fixed by #444
Assignees

Comments

@15864620573
Copy link

15864620573 commented Dec 5, 2023

from blacksheep import Application

parent = Application()


@parent.router.get("/")
def parent_home():
     return "Hello, from the parent app"
 
 
child = Application()
 
 
@child.router.get("/")
def child_home():
     return "Hello, from the child app"


parent.mount_registry.auto_events = True

parent.mount("/sub", child)

For example, in this example, although it is mounted, it will not start and will give a route conflict error.

Why is that? Isn't it already mounted?

@RobertoPrevato
Copy link
Member

RobertoPrevato commented Dec 8, 2023

Hi @15864620573
Thank You for reporting this issue. It is because of a feature I introduced in the second version of the framework to offer a better user experience, reducing code verbosity and making the API to register normal routes identical to the code API to register Controllers routes. The feature is described here: https://www.neoteroi.dev/blacksheep/routing/#using-the-default-router-and-other-routers

In the second version of the framework, when you don´t specify a router for your application, by default it picks the default router - a singleton instantiated in blacksheep itself.

In your example, both the mounted app and the parent app share the same route. This can be fixed instantiating a specific router for (at least) one of the two applications.

from blacksheep import Application, Router

parent = Application(router=Router())


@parent.router.get("/")
def parent_home():
    return "Hello, from the parent app"


child = Application()


@child.router.get("/")
def child_home():
    return "Hello, from the child app"


parent.mount_registry.auto_events = True

parent.mount("/sub", child)

I will improve the documentation regarding mount to clarify this point, and think of issuing warnings / raising a more specific exception.

@RobertoPrevato
Copy link
Member

I forgot to mention, @15864620573, if you want to disable the default singleton router, you can use the Environment Variable APP_DEFAULT_ROUTER and set it to 'false' or '0'.

@RobertoPrevato RobertoPrevato mentioned this issue Dec 9, 2023
RobertoPrevato added a commit that referenced this issue Dec 9, 2023
- Fixes #441 causing the `refresh_token` endpoint for OpenID Connect
  integrations to not work when authentication is required by default.
- Fixes #427, handling WebSocket errors according to ASGI specification.
- Fixes #443, raising a detailed exception when more than one application is
  sharing the same instance of `Router`
- Fixes #438 and #436, restoring support for `uvicorn` used programmatically
  and reloading the application object more than once in the same process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants