-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.py
41 lines (31 loc) · 1.1 KB
/
api-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import anyio
from dotenv import load_dotenv
from hypercorn.asyncio import serve
from hypercorn.config import Config
from main import app
load_dotenv()
def load_config() -> Config:
"""
Load the server configuration from environment variables.
This function reads the 'BIND' environment variable to get the
bind addresses for the server. If 'BIND' is not set, it defaults to '0.0.0.0:8000'.
It returns a Config object with the bind addresses set.
Returns:
Config: A Hypercorn Config object with the bind addresses configured.
"""
bind_addresses = os.getenv("BIND", "0.0.0.0:8000").split(",")
config = Config()
config.bind = bind_addresses
return config
async def main():
"""
Main entry point for the asynchronous server.
This function loads the server configuration and starts the Hypercorn server
using AnyIO's task group to manage the asynchronous tasks.
"""
config = load_config()
async with anyio.create_task_group() as task_group:
task_group.start_soon(serve, app, config)
if __name__ == "__main__":
anyio.run(main)