-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (132 loc) · 4.59 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import aiohttp
import rich
from dotenv import load_dotenv
from src.adapter.external.auth.telegram import TelegramOauthAdapter
# from src.adapter.internal.database.memorydb.service import MemoryDBAdapter
from src.adapter.internal.database.mongodb.service import MongoDBAdapter
from src.app.http.server import build_server
from src.service.allocation import AllocationService
from src.service.answer import AnswerService
from src.service.form_field import FormFieldService
from src.service.participant import ParticipantService
from src.service.preference import PreferenceService
from src.service.room import RoomService
from src.service.user import UserService
from src.utils.logger.logger import Logger
log = Logger("main")
async def notify_start():
version = os.getenv("APP_VERSION", None)
log.info(f"version: {version}")
if version is None or version == "dev":
return
token = os.getenv("STATUS_TELEGRAM_BOT_TOKEN")
if token is None:
return
log.info("status token is present")
chat = os.getenv("STATUS_CHAT_ID")
if chat is None:
return
log.info("status chat id is present")
url = f"https://api.telegram.org/bot{token}/sendMessage"
try:
async with aiohttp.ClientSession() as session:
async with session.post(
url,
data={
"chat_id": chat,
"text": f"🚀 API version <{version}> is online!",
},
) as resp:
resp.raise_for_status()
except Exception as e:
log.error("failed to notify about start: {}", e)
else:
log.info("successfully notified about start")
async def app():
load_dotenv()
telegram_token = os.getenv("TELEGRAM_TOKEN")
if telegram_token is None:
raise RuntimeError("TELEGRAM_TOKEN is not set")
telegram_secret = os.getenv("TELEGRAM_SECRET")
if telegram_secret is None:
raise RuntimeError("TELEGRAM_SECRET is not set")
jwt_secret = os.getenv("JWT_SECRET")
if jwt_secret is None:
raise RuntimeError("JWT_SECRET is not set")
service_secret_key = os.getenv("SERVICE_SECRET_KEY")
if service_secret_key is None:
raise RuntimeError("SERVICE_SECRET_KEY is not set")
base_url = os.getenv("BASE_URL")
if base_url is None:
raise RuntimeError("BASE_URL is not set")
redis_dsn = os.getenv("REDIS_DSN")
if redis_dsn is None:
raise RuntimeError("REDIS_DSN is not set")
mongo_dsn = os.getenv("MONGO_DSN")
if mongo_dsn is None:
raise RuntimeError("MONGO_DSN is not set")
telegram_webhook_url = os.getenv("TELEGRAM_WEBHOOK_URL")
if telegram_webhook_url is None:
raise RuntimeError("TELEGRAM_WEBHOOK_URL is not set")
repo = await MongoDBAdapter.create(mongo_dsn)
user_service = UserService(repo)
allocation_service = AllocationService(
allocation_repo=repo,
form_field_repo=repo,
participant_repo=repo,
user_repo=repo,
)
form_field_service = FormFieldService(
allocation_repo=repo,
form_field_repo=repo,
user_repo=repo,
)
participant_service = ParticipantService(
allocatin_repo=repo,
participant_repo=repo,
room_repo=repo,
user_repo=repo,
)
preference_service = PreferenceService(
preference_repo=repo,
user_repo=repo,
)
room_service = RoomService(
room_repo=repo,
user_repo=repo,
)
answer_service = AnswerService(
form_field_repo=repo,
participant_repo=repo,
)
oauth_adapter = TelegramOauthAdapter(telegram_token, jwt_secret, user_service)
await notify_start()
return build_server(
telegram_token=telegram_token,
telegram_secret=telegram_secret,
webhook_url=telegram_webhook_url,
redis_dsn=redis_dsn,
register_url=base_url + "/register",
fallback_url=base_url,
service_secret_key=service_secret_key,
user_service=user_service,
answer_service=answer_service,
allocation_service=allocation_service,
form_field_service=form_field_service,
participant_service=participant_service,
preference_service=preference_service,
room_service=room_service,
oauth_adapter=oauth_adapter,
)
if __name__ == "__main__":
rich.print(
"You should run",
(
"\t[bold yellow]"
"python3 -m gunicorn main:app --bind 0.0.0.0:8080 --worker-class aiohttp.GunicornWebWorker"
"[/bold yellow]"
),
"to start the server",
sep="\n",
)