inbox/main.py

69 lines
1.9 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
2024-01-24 22:09:11 +00:00
from granian import Granian
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
2024-01-24 22:09:11 +00:00
from granian.server import Interfaces
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-12-23 06:11:04 +00:00
import asyncio
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
from services.core import CacheStorage
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():
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-24 10:06:47 +00:00
if MODE == "dev":
if not exists(DEV_SERVER_PID_FILE_NAME):
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
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-24 10:06:47 +00:00
except Exception as e:
print("STARTUP FAILED")
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])
app.mount("/", GraphQL(schema, debug=True))
2024-01-24 22:09:11 +00:00
if __name__ == "__main__":
Granian(
target="main:app",
2024-01-24 22:34:44 +00:00
port=8888,
2024-01-24 22:09:11 +00:00
interface=Interfaces.ASGI,
reload=True
).serve()