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
|
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
|
2023-11-29 07:23:41 +00:00
|
|
|
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
|
2022-09-03 10:50:14 +00:00
|
|
|
from starlette.applications import Starlette
|
2023-11-29 08:00:00 +00:00
|
|
|
from starlette.endpoints import HTTPEndpoint
|
|
|
|
from starlette.requests import Request
|
2023-11-28 19:07:53 +00:00
|
|
|
from starlette.responses import JSONResponse
|
2023-11-28 21:19:33 +00:00
|
|
|
from starlette.routing import Route
|
2023-11-28 19:07:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-11-29 04:48:31 +00:00
|
|
|
sentry_sdk.init(
|
|
|
|
SENTRY_DSN,
|
|
|
|
enable_tracing=True,
|
2023-11-29 07:23:41 +00:00
|
|
|
integrations=[
|
|
|
|
StarletteIntegration(),
|
|
|
|
AriadneIntegration(),
|
|
|
|
SqlalchemyIntegration(),
|
|
|
|
RedisIntegration(),
|
|
|
|
AioHttpIntegration(),
|
|
|
|
],
|
2023-11-29 04:48:31 +00:00
|
|
|
)
|
|
|
|
|
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):
|
2023-11-28 21:13:46 +00:00
|
|
|
async def post(self, request: Request) -> JSONResponse:
|
2023-11-28 19:07:53 +00:00
|
|
|
try:
|
|
|
|
data = await request.json()
|
|
|
|
if data:
|
2023-11-28 20:13:42 +00:00
|
|
|
auth = request.headers.get("Authorization")
|
|
|
|
if auth:
|
2023-11-28 21:19:33 +00:00
|
|
|
if auth == os.environ.get("WEBHOOK_SECRET"):
|
|
|
|
# Extract user_id and slug
|
|
|
|
user_id = data["user"]["id"]
|
|
|
|
email_slug = data["user"]["email"].replace(".", "-").split("@").pop()
|
|
|
|
slug = data["user"]["preferred_username"] or email_slug
|
|
|
|
await create_author(user_id, slug)
|
2023-11-28 19:07:53 +00:00
|
|
|
return JSONResponse({"status": "success"})
|
|
|
|
except Exception as e:
|
|
|
|
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
|
|
|
|
|
|
|
|
|
2023-11-28 21:19:33 +00:00
|
|
|
routes = [Route("/", GraphQL(schema, debug=True)), Route("/new-author", WebhookEndpoint)]
|
|
|
|
app = Starlette(routes=routes, debug=True, on_startup=[start_up], on_shutdown=[shutdown])
|