core/main.py

48 lines
1.4 KiB
Python
Raw Normal View History

2023-01-17 21:07:44 +00:00
import os
2024-04-01 03:01:38 +00:00
import sentry_sdk
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
2024-02-19 08:58:02 +00:00
from ariadne import load_schema_from_path, make_executable_schema
2022-09-03 10:50:14 +00:00
from ariadne.asgi import GraphQL
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
2024-02-19 11:45:55 +00:00
from services.schema import resolvers
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-02-16 09:34:39 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, MODE
2024-01-25 19:41:27 +00:00
2024-02-21 16:14:58 +00:00
import_module('resolvers')
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)
2024-02-19 08:58:02 +00:00
# Initialize GlitchTip SDK with DSN from environment variable
GLITCHTIP_DSN = os.getenv('GLITCHTIP_DSN')
2024-04-01 20:44:18 +00:00
sentry_sdk.init(GLITCHTIP_DSN)
2022-09-03 10:50:14 +00:00
2024-02-19 07:14:14 +00:00
async def start():
2024-02-21 16:14:58 +00:00
if MODE == 'development':
2024-02-19 07:14:14 +00:00
if not exists(DEV_SERVER_PID_FILE_NAME):
# pid file management
2024-02-21 16:14:58 +00:00
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
2024-02-19 07:14:14 +00:00
f.write(str(os.getpid()))
2024-02-21 16:14:58 +00:00
print(f'[main] process started in {MODE} mode')
2024-02-21 07:27:16 +00:00
2022-09-03 10:50:14 +00:00
2024-02-16 09:40:41 +00:00
# main starlette app object with ariadne mounted in root
app = Starlette(
routes=[
2024-02-21 16:14:58 +00:00
Route('/', GraphQL(schema, debug=True)),
Route('/new-author', WebhookEndpoint),
2024-02-16 09:40:41 +00:00
],
on_startup=[
2024-02-16 09:34:39 +00:00
redis.connect,
ViewedStorage.init,
2024-02-23 10:40:40 +00:00
# search_service.info,
2024-02-20 14:54:43 +00:00
# start_sentry,
2024-02-21 07:27:16 +00:00
start,
2024-02-16 09:40:41 +00:00
],
2024-02-21 07:27:16 +00:00
on_shutdown=[redis.disconnect],
debug=True,
)