-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (56 loc) · 1.7 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
61
62
63
64
65
66
67
68
import sys
import multiprocessing
import asyncio
from hypercorn.config import Config
from hypercorn.asyncio import serve
from socketio import ASGIApp
from loguru import logger
from server import settings, server
async def main():
logger.add(
"./logs/tsh_info.txt",
format="[{time:YYYY-MM-DD HH:mm:ss}] - {level} - {file}:{function}:{line} | {message}",
encoding="utf-8",
level="INFO",
rotation="20 MB"
)
logger.add(
"./logs/tsh_error.txt",
format="[{time:YYYY-MM-DD HH:mm:ss}] - {level} - {file}:{function}:{line} | {message}",
encoding="utf-8",
level="ERROR",
rotation="20 MB"
)
logger.warning("Server is starting")
await settings.load()
config = Config()
config.log.access_logger = logger.bind(name="access_logger")
config.log.error_logger = logger.bind(name="error_logger")
host = await settings.get("server.host")
port = await settings.get("server.port")
config.bind = [f"{host}:{port}"]
await serve(
app=ASGIApp(
server.app.socketio,
server.app
),
config=config,
mode='asgi'
)
return 0
if __name__ == '__main__':
# Pyinstaller fix
multiprocessing.freeze_support()
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
sys.stderr = open('./logs/tsh_error.txt', 'w', encoding='utf-8')
sys.stdout = open('./logs/tsh_info.txt', 'w', encoding='utf-8')
ret = 0
try:
ret = asyncio.run(main())
except asyncio.exceptions.CancelledError:
pass
except:
logger.exception("Exiting application due to exception")
sys.exit(1)
finally:
sys.exit(ret)