-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (43 loc) · 1.33 KB
/
main.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
import asyncio
import logging
import os
import hypercorn
from hypercorn.config import Config
import hypercorn.asyncio
from website import create_app
from website.util import get_db, remove_session_files
from website.core import manager, backround_tasks
from rabbit_mq.send import rpc_client
from rabbit_mq.receive import server
app = create_app()
conn = get_db()
async def start():
logging.info("Starting server")
loop = asyncio.get_event_loop()
await app.app_context().push()
@app.errorhandler(404)
async def not_found(e):
return "This page has not been found", 404
@app.before_serving
async def connect_all_async():
remove_session_files()
await server.connect()
await rpc_client.connect()
await manager.create_clients()
task = loop.create_task(server.start())
backround_tasks.add(task)
@app.after_serving
async def close_conn():
conn.close()
await server.close()
await rpc_client.close()
await manager.close()
for task in backround_tasks:
task.cancel()
config = Config()
port = os.getenv("PORT", 8080)
config.bind = [f"0.0.0.0:{port}"]
await hypercorn.asyncio.serve(app, config)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(start())