inbox/main.py

58 lines
1.6 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
2024-01-25 09:25:52 +00:00
from services.core import CacheStorage
2023-11-22 12:09:24 +00:00
from services.rediscache import redis
2023-12-17 17:13:17 +00:00
from services.schema import resolvers
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
2023-10-03 14:15:17 +00:00
2023-12-23 06:11:04 +00:00
2024-01-25 09:25:52 +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():
2024-01-24 10:06:47 +00:00
try:
await redis.connect()
2023-12-23 06:11:04 +00:00
2024-01-24 10:06:47 +00:00
await CacheStorage.init()
2023-12-23 06:11:04 +00:00
2024-01-25 09:25:52 +00:00
if MODE == 'dev':
2024-01-24 10:06:47 +00:00
if not exists(DEV_SERVER_PID_FILE_NAME):
2024-01-25 09:25:52 +00:00
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
2024-01-24 10:06:47 +00:00
f.write(str(os.getpid()))
else:
# startup sentry monitoring services
2023-12-23 06:11:04 +00:00
import sentry_sdk
sentry_sdk.init(
SENTRY_DSN,
enable_tracing=True,
integrations=[
AriadneIntegration(),
RedisIntegration(),
AioHttpIntegration(),
],
)
2024-01-25 09:25:52 +00:00
except Exception:
print('STARTUP FAILED')
2024-01-24 10:06:47 +00:00
import traceback
traceback.print_exc()
2023-10-03 15:29:56 +00:00
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])
2024-01-25 09:25:52 +00:00
app.mount('/', GraphQL(schema, debug=True))