core/main.py

67 lines
2.1 KiB
Python
Raw Normal View History

2023-01-17 21:07:44 +00:00
import os
2022-09-03 10:50:14 +00:00
from importlib import import_module
2022-11-22 23:51:29 +00:00
from os.path import exists
2023-12-17 20:30:20 +00:00
2022-09-03 10:50:14 +00:00
from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL
2024-01-13 07:27:45 +00:00
2023-12-17 20:30:20 +00:00
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
2023-11-29 04:48:31 +00:00
from sentry_sdk.integrations.ariadne import AriadneIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
2023-11-29 06:14:23 +00:00
from sentry_sdk.integrations.starlette import StarletteIntegration
2022-09-03 10:50:14 +00:00
from starlette.applications import Starlette
2023-11-28 21:19:33 +00:00
from starlette.routing import Route
2023-11-28 19:07:53 +00:00
2023-12-17 04:59:16 +00:00
from resolvers.webhook import WebhookEndpoint
2023-10-23 14:47:11 +00:00
from services.rediscache import redis
2023-10-11 09:23:09 +00:00
from services.schema import resolvers
2023-12-17 20:30:20 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
2023-12-22 09:09:24 +00:00
from services.viewed import ViewedStorage
2022-09-03 10:50:14 +00:00
import_module("resolvers")
2023-10-05 18:46:18 +00:00
schema = make_executable_schema(load_schema_from_path("schemas/core.graphql"), resolvers) # type: ignore
2022-09-03 10:50:14 +00:00
async def start_up():
2023-12-24 14:25:57 +00:00
print(f"[main] starting in {MODE} mode")
2023-12-22 09:09:24 +00:00
await redis.connect()
2023-12-23 05:40:41 +00:00
# start viewed service
await ViewedStorage.init()
2023-12-22 09:09:24 +00:00
2023-10-23 14:47:11 +00:00
if MODE == "development":
2023-12-22 09:09:24 +00:00
# pid file management
if not exists(DEV_SERVER_PID_FILE_NAME):
2023-10-23 14:47:11 +00:00
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
f.write(str(os.getpid()))
2023-12-22 09:09:24 +00:00
if MODE == "production":
# sentry monitoring
try:
import sentry_sdk
sentry_sdk.init(
SENTRY_DSN,
enable_tracing=True,
integrations=[
StarletteIntegration(),
AriadneIntegration(),
SqlalchemyIntegration(),
RedisIntegration(),
AioHttpIntegration(),
],
)
except Exception as e:
print("[sentry] init error")
print(e)
2022-09-03 10:50:14 +00:00
async def shutdown():
await redis.disconnect()
2023-11-28 21:19:33 +00:00
routes = [Route("/", GraphQL(schema, debug=True)), Route("/new-author", WebhookEndpoint)]
app = Starlette(routes=routes, debug=True, on_startup=[start_up], on_shutdown=[shutdown])