-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiohttp_server.py
65 lines (44 loc) · 1.21 KB
/
aiohttp_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
from typing import AsyncIterator
from aiohttp import web
from darq.app import Darq
from darq.connections import RedisSettings
from darq_ui.integration.aiohttp import setup
log = logging.getLogger(__name__)
darq = Darq(
redis_settings=RedisSettings(
host="localhost",
port=6379,
database=0,
),
ctx={},
)
@darq.task
async def say_hello_task(name: str) -> None:
log.info("Hello %s", name)
async def lifecycle(app) -> AsyncIterator[None]:
await darq.connect()
yield
await darq.disconnect()
def create_app() -> web.Application:
app = web.Application()
app.cleanup_ctx.append(lifecycle)
# In order for this to work, you need to run npm run build or npm run build-watch at least once
setup(
app,
darq,
base_path="/",
logs_url="https://mylogserver.com/taskname=${taskName}",
)
log.info("Server api configured at http:0.0.0.0:3000/api endpoint")
return app
def main() -> None:
app = create_app()
log.info(
"darq-ui example aiohttp server started: %s:%d",
"localhost",
3000,
)
web.run_app(app, host="localhost", port=3000)
if __name__ == "__main__":
main()