|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +from hypercorn.asyncio import serve |
| 4 | +from hypercorn.config import Config |
1 | 5 | from starlette.applications import Starlette |
2 | 6 | from starlette.routing import Route |
3 | 7 |
|
4 | 8 | from coagent.cos.runtime import CosRuntime |
5 | | -from coagent.runtimes import NATSRuntime, LocalRuntime |
6 | | -from coagent.core.util import get_nats_servers |
| 9 | +from coagent.core import set_stderr_logger |
| 10 | +from coagent.runtimes import NATSRuntime, HTTPRuntime |
7 | 11 |
|
8 | | -runtime = CosRuntime(NATSRuntime.from_servers(servers=get_nats_servers())) |
9 | 12 |
|
| 13 | +class Application: |
| 14 | + def __init__(self, server: str, auth: str): |
| 15 | + if server.startswith("nats://"): |
| 16 | + runtime = NATSRuntime.from_servers(server) |
| 17 | + elif server.startswith(("http://", "https://")): |
| 18 | + runtime = HTTPRuntime.from_server(server, auth) |
| 19 | + else: |
| 20 | + raise ValueError(f"Unsupported server: {server}") |
| 21 | + self.runtime = CosRuntime(runtime) |
10 | 22 |
|
11 | | -async def startup(): |
12 | | - await runtime.start() |
| 23 | + async def startup(self): |
| 24 | + await self.runtime.start() |
13 | 25 |
|
| 26 | + async def shutdown(self): |
| 27 | + await self.runtime.stop() |
14 | 28 |
|
15 | | -async def shutdown(): |
16 | | - await runtime.stop() |
17 | | - |
18 | | - |
19 | | -routes = [ |
20 | | - Route("/runtime/register", runtime.register, methods=["POST"]), |
21 | | - Route("/runtime/channel/subscribe", runtime.subscribe, methods=["POST"]), |
22 | | - Route("/runtime/channel/publish", runtime.publish, methods=["POST"]), |
23 | | - Route("/runtime/channel/publish_multi", runtime.publish_multi, methods=["POST"]), |
24 | | -] |
25 | | - |
26 | | - |
27 | | -app = Starlette(debug=True, routes=routes, on_startup=[startup], on_shutdown=[shutdown]) |
| 29 | + @property |
| 30 | + def starlette(self) -> Starlette: |
| 31 | + routes = [ |
| 32 | + Route( |
| 33 | + "/runtime/register", |
| 34 | + self.runtime.register, |
| 35 | + methods=["POST"], |
| 36 | + ), |
| 37 | + Route( |
| 38 | + "/runtime/channel/subscribe", |
| 39 | + self.runtime.subscribe, |
| 40 | + methods=["POST"], |
| 41 | + ), |
| 42 | + Route( |
| 43 | + "/runtime/channel/publish", |
| 44 | + self.runtime.publish, |
| 45 | + methods=["POST"], |
| 46 | + ), |
| 47 | + Route( |
| 48 | + "/runtime/channel/publish_multi", |
| 49 | + self.runtime.publish_multi, |
| 50 | + methods=["POST"], |
| 51 | + ), |
| 52 | + ] |
| 53 | + return Starlette( |
| 54 | + debug=True, |
| 55 | + routes=routes, |
| 56 | + on_startup=[self.startup], |
| 57 | + on_shutdown=[self.shutdown], |
| 58 | + ) |
28 | 59 |
|
29 | 60 |
|
30 | 61 | if __name__ == "__main__": |
31 | | - import asyncio |
32 | | - from hypercorn.asyncio import serve |
33 | | - from hypercorn.config import Config |
| 62 | + parser = argparse.ArgumentParser() |
| 63 | + parser.add_argument( |
| 64 | + "--listen", |
| 65 | + type=str, |
| 66 | + default="127.0.0.1:8000", |
| 67 | + help="The listen address of the CoS server. (Defaults to %(default)r)", |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + "--server", |
| 71 | + type=str, |
| 72 | + default="nats://localhost:4222", |
| 73 | + help="The runtime server address. (Defaults to %(default)r)", |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--auth", |
| 77 | + type=str, |
| 78 | + default="", |
| 79 | + help="The runtime server authentication token. (Defaults to %(default)r)", |
| 80 | + ) |
| 81 | + args = parser.parse_args() |
| 82 | + |
| 83 | + set_stderr_logger() |
| 84 | + app = Application(args.server, args.auth).starlette |
34 | 85 |
|
35 | 86 | config = Config() |
36 | | - config.bind = ["127.0.0.1:8000"] |
| 87 | + config.bind = [args.listen] |
37 | 88 | asyncio.run(serve(app, config)) |
0 commit comments