-
Notifications
You must be signed in to change notification settings - Fork 104
/
main.py
112 lines (88 loc) · 2.53 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import logging
from telemirror.mirroring import Telemirror
from telemirror.storage import InMemoryDatabase, PostgresDatabase
async def serve_health_endpoint(host: str, port: int) -> None:
from aiohttp import web
async def health(_):
return web.Response(text="OK")
app = web.Application()
app.add_routes([web.get("/", health)])
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host, port)
await site.start()
def configure_logging(logger_name: str, log_level: str) -> logging.Logger:
logger = logging.getLogger(logger_name)
logger.setLevel(log_level)
if not logger.handlers:
import sys
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
handler.setFormatter(
logging.Formatter(
"%(levelname)-5s %(asctime)s [%(filename)s:%(lineno)d]:%(name)s: %(message)s"
)
)
logger.addHandler(handler)
return logger
async def run_telemirror(
use_memory_db: bool,
db_uri: str,
api_id: str,
api_hash: str,
session_string: str,
chat_mapping: dict,
logger: logging.Logger,
host: str,
port: int,
):
await serve_health_endpoint(host=host, port=port)
if use_memory_db:
database = InMemoryDatabase()
else:
database = await PostgresDatabase(connection_string=db_uri)
telemirror = Telemirror(
api_id=api_id,
api_hash=api_hash,
session_string=session_string,
chat_mapping=chat_mapping,
database=database,
logger=logger,
)
await telemirror.run()
def main():
import asyncio
import sys
from config import (
API_HASH,
API_ID,
CHAT_MAPPING,
DB_URL,
HOST,
LOG_LEVEL,
PORT,
SESSION_STRING,
USE_MEMORY_DB,
)
if sys.platform == "win32":
if USE_MEMORY_DB is False:
# required by psycopg async pool on windows platform
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
else:
import uvloop
uvloop.install()
asyncio.run(
run_telemirror(
use_memory_db=USE_MEMORY_DB,
db_uri=DB_URL,
api_id=API_ID,
api_hash=API_HASH,
session_string=SESSION_STRING,
chat_mapping=CHAT_MAPPING,
logger=configure_logging("telemirror", LOG_LEVEL),
host=HOST,
port=PORT,
)
)
if __name__ == "__main__":
main()