Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.8k stars 75 forks source link

Mounting applications #486

Closed lbsx closed 3 months ago

lbsx commented 4 months ago
import uvicorn
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"

# Note: when mounting another BlackSheep application,
# make sure to handle the start and stop events of the mounted app
parent.mount_registry.auto_events = True

parent.mount("/sub", child)

if __name__ == "__main__":
    uvicorn.run("multiapp:app", host="0.0.0.0", port=5556)

I got the code from blacksheep When I run it, it throws an error, Traceback (most recent call last): File "/home/leib/code/py/web/xy/multiapp.py", line 12, in child = Application() File "/home/leib/.pyenv/versions/3.10.13/lib/python3.10/site-packages/blacksheep/server/application.py", line 213, in init validate_router(self) File "/home/leib/.pyenv/versions/3.10.13/lib/python3.10/site-packages/blacksheep/server/routing.py", line 963, in validate_router raise SharedRouterError() blacksheep.server.routing.SharedRouterError: Invalid routers configuration: the same router is used in more than one Application object. When working with multiple applications, ensure that each application is configured to use different routers. For more information, refer to: https://www.neoteroi.dev/blacksheep/routing/

mingwiki commented 4 months ago

Invalid routers configuration: the same router is used in more than one Application object. 搞了两个"/",路由重复了,写的清清楚楚

lbsx commented 4 months ago

@mingwiki 那个代码是https://www.neoteroi.dev/blacksheep/mounting/ 里的官方示例,把路由改成其他的,也一样报这个错,把路由全删了只留两个app也报这个错.

sinisaos commented 3 months ago

@lbsx The documentation has not been updated, but fix is to instantiate a specific router in one of your applications as described in this issue.

lbsx commented 3 months ago

@sinisaos Thank you for helping me solve my doubts