2023-11-23 22:58:55 +00:00
|
|
|
import os
|
2023-11-24 02:18:02 +00:00
|
|
|
import asyncio
|
2023-11-23 22:58:55 +00:00
|
|
|
from os.path import exists
|
|
|
|
|
2023-11-30 06:42:41 +00:00
|
|
|
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
|
|
|
|
from sentry_sdk.integrations.redis import RedisIntegration
|
|
|
|
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
|
2023-11-26 10:51:06 +00:00
|
|
|
from sentry_sdk.integrations.strawberry import StrawberryIntegration
|
2023-11-26 10:18:57 +00:00
|
|
|
from strawberry.asgi import GraphQL
|
2023-11-23 22:58:55 +00:00
|
|
|
from starlette.applications import Starlette
|
|
|
|
|
2023-12-17 11:42:04 +00:00
|
|
|
from services.auth import TokenMiddleware
|
2023-11-23 22:58:55 +00:00
|
|
|
from services.rediscache import redis
|
2023-11-26 11:54:07 +00:00
|
|
|
from resolvers.listener import reactions_worker
|
2023-11-26 10:18:57 +00:00
|
|
|
from resolvers.schema import schema
|
2023-11-23 22:58:55 +00:00
|
|
|
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
|
|
|
|
|
|
|
|
|
|
|
|
async def start_up():
|
|
|
|
if MODE == "dev":
|
|
|
|
if exists(DEV_SERVER_PID_FILE_NAME):
|
|
|
|
await redis.connect()
|
|
|
|
else:
|
|
|
|
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
|
|
|
|
f.write(str(os.getpid()))
|
|
|
|
else:
|
|
|
|
await redis.connect()
|
2023-11-26 11:54:07 +00:00
|
|
|
notification_service_task = asyncio.create_task(reactions_worker())
|
2023-11-26 18:21:14 +00:00
|
|
|
print(f"[main.start_up] {notification_service_task}")
|
|
|
|
try:
|
|
|
|
import sentry_sdk
|
|
|
|
|
|
|
|
sentry_sdk.init(
|
|
|
|
SENTRY_DSN,
|
|
|
|
enable_tracing=True,
|
|
|
|
integrations=[
|
2023-12-15 12:36:43 +00:00
|
|
|
StrawberryIntegration(async_execution=True),
|
2023-11-30 06:42:41 +00:00
|
|
|
SqlalchemyIntegration(),
|
|
|
|
RedisIntegration(),
|
|
|
|
AioHttpIntegration(),
|
2023-11-26 18:21:14 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
print("[sentry] init error")
|
|
|
|
print(e)
|
2023-11-23 22:58:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def shutdown():
|
|
|
|
await redis.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
|
2023-12-17 11:42:04 +00:00
|
|
|
app.add_middleware(TokenMiddleware)
|
2023-11-23 22:58:55 +00:00
|
|
|
app.mount("/", GraphQL(schema, debug=True))
|