core/main.py

96 lines
3.0 KiB
Python
Raw Normal View History

2024-10-14 08:11:13 +00:00
import asyncio
2023-01-17 21:07:44 +00:00
import os
2024-12-11 22:04:11 +00:00
import sys
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
2024-12-11 22:04:11 +00:00
from starlette.middleware.cors import CORSMiddleware
2024-10-14 09:31:55 +00:00
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
2024-10-14 09:19:30 +00:00
from starlette.routing import Route
2023-11-28 19:07:53 +00:00
2024-08-09 06:37:06 +00:00
from cache.precache import precache_data
from cache.revalidator import revalidation_manager
2024-06-04 06:07:46 +00:00
from services.exception import ExceptionHandlerMiddleware
2024-08-09 06:37:06 +00:00
from services.redis import redis
2025-02-10 15:04:08 +00:00
from services.schema import create_all_tables, resolvers
2024-05-18 08:28:38 +00:00
from services.search import search_service
2023-12-22 09:09:24 +00:00
from services.viewed import ViewedStorage
2024-12-11 19:10:48 +00:00
from services.webhook import WebhookEndpoint, create_webhook_endpoint
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-04-17 15:32:23 +00:00
import_module("resolvers")
schema = make_executable_schema(load_schema_from_path("schema/"), resolvers)
2024-02-19 08:58:02 +00:00
2022-09-03 10:50:14 +00:00
2024-02-19 07:14:14 +00:00
async def start():
2024-04-17 15:32:23 +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-04-17 15:32:23 +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-04-17 15:32:23 +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
2025-02-10 15:15:54 +00:00
async def lifespan(_app):
2024-11-01 21:26:57 +00:00
try:
2025-02-10 15:15:54 +00:00
create_all_tables()
2024-11-01 21:26:57 +00:00
await asyncio.gather(
redis.connect(),
precache_data(),
ViewedStorage.init(),
2024-12-11 19:10:48 +00:00
create_webhook_endpoint(),
2024-12-11 19:31:41 +00:00
search_service.info(),
2024-11-01 21:26:57 +00:00
start(),
revalidation_manager.start(),
)
yield
finally:
2024-11-02 09:09:24 +00:00
tasks = [redis.disconnect(), ViewedStorage.stop(), revalidation_manager.stop()]
2024-11-01 21:26:57 +00:00
await asyncio.gather(*tasks, return_exceptions=True)
2024-10-14 09:13:18 +00:00
# Создаем экземпляр GraphQL
graphql_app = GraphQL(schema, debug=True)
2024-10-14 09:19:30 +00:00
2024-10-14 08:11:13 +00:00
# Оборачиваем GraphQL-обработчик для лучшей обработки ошибок
2024-10-14 09:31:55 +00: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 08:11:13 +00:00
try:
2024-10-14 09:31:55 +00:00
result = await graphql_app.handle_request(request)
if isinstance(result, Response):
return result
return JSONResponse(result)
2024-10-14 08:11:13 +00:00
except asyncio.CancelledError:
return JSONResponse({"error": "Request cancelled"}, status_code=499)
except Exception as e:
2024-10-14 09:31:55 +00:00
print(f"GraphQL error: {str(e)}")
2024-10-14 08:11:13 +00:00
return JSONResponse({"error": str(e)}, status_code=500)
2024-10-14 09:31:55 +00:00
# Обновляем маршрут в Starlette
2024-02-16 09:40:41 +00:00
app = Starlette(
routes=[
2024-10-14 09:31:55 +00:00
Route("/", graphql_handler, methods=["GET", "POST"]),
2024-04-17 15:32:23 +00:00
Route("/new-author", WebhookEndpoint),
2024-02-16 09:40:41 +00:00
],
2024-10-14 09:13:18 +00:00
lifespan=lifespan,
2024-02-21 07:27:16 +00:00
debug=True,
2024-04-08 06:17:05 +00:00
)
2024-10-14 09:13:18 +00:00
2024-06-04 05:15:59 +00:00
app.add_middleware(ExceptionHandlerMiddleware)
2024-12-11 22:04:11 +00:00
if "dev" in sys.argv:
app.add_middleware(
CORSMiddleware,
allow_origins=["https://localhost:3000"],
2024-12-11 22:04:11 +00:00
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)