core/main.py

56 lines
1.6 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
2022-09-03 10:50:14 +00:00
from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL
from starlette.applications import Starlette
2023-11-28 19:07:53 +00:00
from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from resolvers.author import create_author
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-10-23 14:47:11 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, MODE
2022-11-22 23:51:29 +00:00
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-10-23 14:47:11 +00:00
if MODE == "development":
if exists(DEV_SERVER_PID_FILE_NAME):
await redis.connect()
return
else:
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
f.write(str(os.getpid()))
else:
await redis.connect()
2022-12-04 08:24:43 +00:00
try:
import sentry_sdk
2023-10-23 14:47:11 +00:00
2022-12-04 14:03:55 +00:00
sentry_sdk.init(SENTRY_DSN)
2022-12-04 08:24:43 +00:00
except Exception as e:
2023-10-05 18:46:18 +00:00
print("[sentry] init error")
2022-12-04 08:24:43 +00:00
print(e)
2022-09-03 10:50:14 +00:00
async def shutdown():
await redis.disconnect()
2023-11-28 19:07:53 +00:00
class WebhookEndpoint(HTTPEndpoint):
async def post(self, request):
try:
data = await request.json()
if data:
await create_author(data)
return JSONResponse({"status": "success"})
except Exception as e:
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
2023-10-23 14:47:11 +00:00
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
app.mount("/", GraphQL(schema, debug=True))
2023-11-28 19:07:53 +00:00
app.mount("/new-author", WebhookEndpoint)