core/main.py

76 lines
2.2 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
import sentry_sdk
2022-09-03 10:50:14 +00:00
from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL
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-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
2024-01-29 03:45:07 +00:00
from services.search import search_service
2023-12-22 09:09:24 +00:00
from services.viewed import ViewedStorage
2024-02-02 12:03:44 +00:00
from services.webhook import WebhookEndpoint
2024-01-25 19:41:27 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
2023-12-22 09:09:24 +00:00
2024-01-25 19:41:27 +00:00
import_module('resolvers')
2024-02-02 12:03:44 +00:00
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)
2022-09-03 10:50:14 +00:00
async def start_up():
2024-01-25 19:41:27 +00:00
print(f'[main] starting in {MODE} mode')
2023-12-24 14:25:57 +00:00
with sentry_sdk.start_transaction(op='task', name='Redis Connection'):
await redis.connect()
2023-12-22 09:09:24 +00:00
2023-12-23 05:40:41 +00:00
# start viewed service
await ViewedStorage.init()
2023-12-22 09:09:24 +00:00
2024-02-03 09:48:36 +00:00
2024-01-29 00:27:30 +00:00
# start search service
2024-01-29 03:45:07 +00:00
search_service.info()
2024-01-29 00:27:30 +00:00
2024-01-25 19:41:27 +00:00
if MODE == 'development':
2023-12-22 09:09:24 +00:00
# pid file management
if not exists(DEV_SERVER_PID_FILE_NAME):
2024-01-25 19:41:27 +00:00
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
2023-10-23 14:47:11 +00:00
f.write(str(os.getpid()))
2023-12-22 09:09:24 +00:00
2024-01-25 19:41:27 +00:00
if MODE == 'production':
2023-12-22 09:09:24 +00:00
# sentry monitoring
try:
sentry_sdk.init(
SENTRY_DSN,
enable_tracing=True,
integrations=[
StarletteIntegration(),
AriadneIntegration(),
SqlalchemyIntegration(),
RedisIntegration(),
AioHttpIntegration(),
],
)
except Exception as e:
2024-01-25 19:41:27 +00:00
print('[sentry] init error')
2023-12-22 09:09:24 +00:00
print(e)
2022-09-03 10:50:14 +00:00
async def shutdown():
await redis.disconnect()
2024-01-29 00:27:30 +00:00
routes = [
Route('/', GraphQL(schema, debug=True)),
Route('/new-author', WebhookEndpoint),
]
2024-01-29 04:04:37 +00:00
app = Starlette(routes=routes, debug=True, on_startup=[start_up], on_shutdown=[shutdown])