inbox/main.py

53 lines
1.5 KiB
Python
Raw Normal View History

2023-10-03 15:29:56 +00:00
import os
2023-10-04 21:43:07 +00:00
from importlib import import_module
2023-10-03 15:29:56 +00:00
from os.path import exists
2023-10-14 12:59:43 +00:00
2023-10-03 15:29:56 +00:00
from ariadne import load_schema_from_path, make_executable_schema
2023-10-03 14:15:17 +00:00
from ariadne.asgi import GraphQL
2023-11-30 06:51:22 +00:00
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.ariadne import AriadneIntegration
from sentry_sdk.integrations.redis import RedisIntegration
2023-10-03 15:29:56 +00:00
from starlette.applications import Starlette
2023-10-14 12:59:43 +00:00
2023-10-04 21:46:16 +00:00
from services.schema import resolvers
2023-11-22 12:09:24 +00:00
from services.rediscache import redis
2023-10-04 20:42:39 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
2023-10-03 14:15:17 +00:00
2023-10-04 21:43:07 +00:00
import_module("resolvers")
schema = make_executable_schema(load_schema_from_path("inbox.graphql"), resolvers) # type: ignore
2023-10-03 14:15:17 +00:00
2023-10-03 15:29:56 +00:00
async def start_up():
if MODE == "dev":
if exists(DEV_SERVER_PID_FILE_NAME):
await redis.connect()
return
else:
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
f.write(str(os.getpid()))
else:
await redis.connect()
try:
import sentry_sdk
2023-10-03 14:15:17 +00:00
2023-11-30 06:51:22 +00:00
sentry_sdk.init(
SENTRY_DSN,
enable_tracing=True,
integrations=[
AriadneIntegration(),
RedisIntegration(),
AioHttpIntegration(),
],
)
2023-10-03 15:29:56 +00:00
except Exception as e:
print("[sentry] init error")
print(e)
async def shutdown():
2023-10-03 14:15:17 +00:00
await redis.disconnect()
2023-10-03 15:29:56 +00:00
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
app.mount("/", GraphQL(schema, debug=True))