forked from khoben/telemirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (104 loc) · 3.11 KB
/
main.py
File metadata and controls
129 lines (104 loc) · 3.11 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import logging
from telemirror.mirroring import Telemirror
from telemirror.storage import InMemoryDatabase, PostgresDatabase
async def serve_health_endpoint(host: str, port: int) -> None:
"""
Start http health endpoint at /.
Some PaaS providers require a health endpoint to verify that the service has started successfully.
"""
from aiohttp import web
async def health(_):
return web.Response(status=204)
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,
api_device_model: str,
api_system_version: str,
api_app_version: 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,
api_device_model=api_device_model,
api_system_version=api_system_version,
api_app_version=api_app_version,
)
await telemirror.run()
def main():
import asyncio
import sys
from config import (
API_APP_VERSION,
API_DEVICE_MODEL,
API_HASH,
API_ID,
API_SYSTEM_VERSION,
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,
api_device_model=API_DEVICE_MODEL,
api_system_version=API_SYSTEM_VERSION,
api_app_version=API_APP_VERSION,
session_string=SESSION_STRING,
chat_mapping=CHAT_MAPPING,
logger=configure_logging("telemirror", LOG_LEVEL),
host=HOST,
port=PORT,
)
)
if __name__ == "__main__":
main()