Files
core/main.py

125 lines
3.7 KiB
Python
Raw Normal View History

2024-10-14 11:11:13 +03:00
import asyncio
2023-01-17 22:07:44 +01:00
import os
2022-09-03 13:50:14 +03:00
from importlib import import_module
2022-11-23 00:51:29 +01:00
from os.path import exists
2023-12-17 23:30:20 +03:00
2024-02-19 11:58:02 +03:00
from ariadne import load_schema_from_path, make_executable_schema
2022-09-03 13:50:14 +03:00
from ariadne.asgi import GraphQL
from starlette.applications import Starlette
2024-10-14 12:31:55 +03:00
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
2024-10-14 12:19:30 +03:00
from starlette.routing import Route
2023-11-28 22:07:53 +03:00
2024-08-09 09:37:06 +03:00
from cache.precache import precache_data
from cache.revalidator import revalidation_manager
2024-10-14 12:31:55 +03:00
from orm import (
# collection,
# invite,
2024-10-14 12:19:30 +03:00
author,
community,
notification,
reaction,
shout,
topic,
)
from services.db import create_table_if_not_exists, engine
2024-06-04 09:07:46 +03:00
from services.exception import ExceptionHandlerMiddleware
2024-08-09 09:37:06 +03:00
from services.redis import redis
2024-02-19 14:45:55 +03:00
from services.schema import resolvers
2024-05-18 11:28:38 +03:00
from services.search import search_service
2023-12-22 12:09:24 +03:00
from services.viewed import ViewedStorage
2024-02-02 15:03:44 +03:00
from services.webhook import WebhookEndpoint
2024-02-16 12:34:39 +03:00
from settings import DEV_SERVER_PID_FILE_NAME, MODE
2024-01-25 22:41:27 +03:00
2024-04-17 18:32:23 +03:00
import_module("resolvers")
schema = make_executable_schema(load_schema_from_path("schema/"), resolvers)
2024-02-19 11:58:02 +03:00
2022-09-03 13:50:14 +03:00
2024-02-19 10:14:14 +03:00
async def start():
2024-04-17 18:32:23 +03:00
if MODE == "development":
2024-02-19 10:14:14 +03:00
if not exists(DEV_SERVER_PID_FILE_NAME):
# pid file management
2024-04-17 18:32:23 +03:00
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
2024-02-19 10:14:14 +03:00
f.write(str(os.getpid()))
2024-04-17 18:32:23 +03:00
print(f"[main] process started in {MODE} mode")
2024-02-21 10:27:16 +03:00
2022-09-03 13:50:14 +03:00
2024-10-14 12:19:30 +03:00
def create_all_tables():
for model in [
2024-10-15 19:50:17 +03:00
# user.User,
2024-10-14 12:19:30 +03:00
author.Author,
author.AuthorFollower,
2024-10-21 20:21:31 +03:00
community.Community,
community.CommunityFollower,
2024-10-14 12:19:30 +03:00
shout.Shout,
shout.ShoutAuthor,
2024-10-21 12:15:44 +03:00
author.AuthorBookmark,
2024-10-14 12:19:30 +03:00
topic.Topic,
topic.TopicFollower,
2024-10-15 19:50:17 +03:00
shout.ShoutTopic,
2024-10-14 12:19:30 +03:00
reaction.Reaction,
2024-10-15 19:50:17 +03:00
shout.ShoutReactionsFollower,
author.AuthorRating,
notification.Notification,
notification.NotificationSeen,
2024-10-14 12:19:30 +03:00
# collection.Collection, collection.ShoutCollection,
# invite.Invite
]:
create_table_if_not_exists(engine, model)
async def create_all_tables_async():
# Оборачиваем синхронную функцию в асинхронную
await asyncio.to_thread(create_all_tables)
2024-10-14 12:13:18 +03:00
async def lifespan(app):
2024-11-02 00:26:57 +03:00
try:
await asyncio.gather(
create_all_tables_async(),
redis.connect(),
precache_data(),
ViewedStorage.init(),
search_service.info(),
start(),
revalidation_manager.start(),
)
yield
finally:
2024-11-02 12:09:24 +03:00
tasks = [redis.disconnect(), ViewedStorage.stop(), revalidation_manager.stop()]
2024-11-02 00:26:57 +03:00
await asyncio.gather(*tasks, return_exceptions=True)
2024-10-14 12:13:18 +03:00
# Создаем экземпляр GraphQL
graphql_app = GraphQL(schema, debug=True)
2024-10-14 12:19:30 +03:00
2024-10-14 11:11:13 +03:00
# Оборачиваем GraphQL-обработчик для лучшей обработки ошибок
2024-10-14 12:31:55 +03:00
async def graphql_handler(request: Request):
if request.method not in ["GET", "POST"]:
return JSONResponse({"error": "Method Not Allowed"}, status_code=405)
2024-10-14 11:11:13 +03:00
try:
2024-10-14 12:31:55 +03:00
result = await graphql_app.handle_request(request)
if isinstance(result, Response):
return result
return JSONResponse(result)
2024-10-14 11:11:13 +03:00
except asyncio.CancelledError:
return JSONResponse({"error": "Request cancelled"}, status_code=499)
except Exception as e:
2024-10-14 12:31:55 +03:00
print(f"GraphQL error: {str(e)}")
2024-10-14 11:11:13 +03:00
return JSONResponse({"error": str(e)}, status_code=500)
2024-10-14 12:31:55 +03:00
# Обновляем маршрут в Starlette
2024-02-16 12:40:41 +03:00
app = Starlette(
routes=[
2024-10-14 12:31:55 +03:00
Route("/", graphql_handler, methods=["GET", "POST"]),
2024-04-17 18:32:23 +03:00
Route("/new-author", WebhookEndpoint),
2024-02-16 12:40:41 +03:00
],
2024-10-14 12:13:18 +03:00
lifespan=lifespan,
2024-02-21 10:27:16 +03:00
debug=True,
2024-04-08 09:17:05 +03:00
)
2024-10-14 12:13:18 +03:00
2024-06-04 08:15:59 +03:00
app.add_middleware(ExceptionHandlerMiddleware)