core/main.py

94 lines
2.5 KiB
Python
Raw Normal View History

import asyncio
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
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.routing import Route
2022-11-19 11:35:34 +00:00
from orm import init_tables
2022-09-03 10:50:14 +00:00
from auth.authenticate import JWTAuthenticate
from auth.oauth import oauth_login, oauth_authorize
2022-10-05 17:06:29 +00:00
from resolvers.auth import confirm_email_handler
2023-07-13 13:01:58 +00:00
from resolvers.upload import upload_handler
2023-01-17 21:07:44 +00:00
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN
2023-10-06 00:22:37 +00:00
from services.search import SearchService
from services.viewed import ViewedStorage
from services.db import local_session
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
middleware = [
Middleware(AuthenticationMiddleware, backend=JWTAuthenticate()),
Middleware(SessionMiddleware, secret_key=SESSION_SECRET_KEY),
2022-09-03 10:50:14 +00:00
]
async def start_up():
2022-11-19 11:35:34 +00:00
init_tables()
2022-09-03 10:50:14 +00:00
await redis.connect()
2023-10-06 00:22:37 +00:00
with local_session() as session:
await SearchService.init(session)
await ViewedStorage.init()
_views_stat_task = asyncio.create_task(ViewedStorage().worker())
2022-12-04 08:24:43 +00:00
try:
import sentry_sdk
2022-12-04 14:03:55 +00:00
sentry_sdk.init(SENTRY_DSN)
2023-10-05 22:12:34 +00:00
print("[sentry] started")
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
2023-10-09 21:22:16 +00:00
print("[main] started")
2022-11-25 18:31:53 +00:00
2022-11-22 23:51:29 +00:00
async def dev_start_up():
2023-01-17 21:07:44 +00:00
if exists(DEV_SERVER_PID_FILE_NAME):
2022-12-01 13:24:05 +00:00
await redis.connect()
2022-11-22 23:51:29 +00:00
return
else:
2023-10-05 18:46:18 +00:00
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
2023-01-17 21:07:44 +00:00
f.write(str(os.getpid()))
2022-11-22 23:51:29 +00:00
await start_up()
2022-09-03 10:50:14 +00:00
async def shutdown():
await redis.disconnect()
routes = [
Route("/oauth/{provider}", endpoint=oauth_login),
2022-10-05 16:52:17 +00:00
Route("/oauth-authorize", endpoint=oauth_authorize),
2023-07-13 13:01:58 +00:00
Route("/confirm/{token}", endpoint=confirm_email_handler),
2023-10-05 18:46:18 +00:00
Route("/upload", endpoint=upload_handler, methods=["POST"]),
2022-09-03 10:50:14 +00:00
]
app = Starlette(
debug=True,
on_startup=[start_up],
on_shutdown=[shutdown],
middleware=middleware,
routes=routes,
)
2022-12-14 08:35:47 +00:00
app.mount("/", GraphQL(
2022-12-04 14:03:55 +00:00
schema,
debug=True
2022-12-04 14:03:55 +00:00
))
2023-10-09 21:29:22 +00:00
dev_app = Starlette(
2022-11-22 23:51:29 +00:00
debug=True,
on_startup=[dev_start_up],
2022-12-01 13:24:05 +00:00
on_shutdown=[shutdown],
2022-11-22 23:51:29 +00:00
middleware=middleware,
routes=routes,
)
2023-10-05 18:46:18 +00:00
dev_app.mount(
"/",
GraphQL(schema, debug=True),
)