-
|
Hey! Is it possible to expose multiple services by the same server? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @Laci556! Yes, you can absolutely serve multiple services on the same server. ASGI applications are composable, and connect-python is designed with this in mind - each generated application class (e.g., Using Starlette (recommended)from starlette.applications import Starlette
from starlette.routing import Mount
from your_service1_connect import Service1ASGIApplication
from your_service2_connect import Service2ASGIApplication
service1_app = Service1ASGIApplication(YourService1Impl())
service2_app = Service2ASGIApplication(YourService2Impl())
app = Starlette(
routes=[
Mount(service1_app.path, service1_app),
Mount(service2_app.path, service2_app),
]
)Without any extra dependenciesfrom your_service1_connect import Service1ASGIApplication
from your_service2_connect import Service2ASGIApplication
service1_app = Service1ASGIApplication(YourService1Impl())
service2_app = Service2ASGIApplication(YourService2Impl())
apps = {
service1_app.path: service1_app,
service2_app.path: service2_app,
}
async def app(scope, receive, send):
if scope["type"] != "http":
return
path = scope["path"]
for prefix, service_app in apps.items():
if path.startswith(prefix):
await service_app(scope, receive, send)
return
await send({"type": "http.response.start", "status": 404, "headers": []})
await send({"type": "http.response.body", "body": b"Not Found"})Note: The dependency-free version skips lifespan handling for simplicity. If you need proper lifespan support (for startup/shutdown hooks), Starlette is recommended as it handles this correctly. You can also check out Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi @Laci556!
Yes, you can absolutely serve multiple services on the same server. ASGI applications are composable, and connect-python is designed with this in mind - each generated application class (e.g.,
ElizaServiceASGIApplication) has a.pathproperty specifically for mounting.Using Starlette (recommended)