Files
core/main.py

97 lines
2.6 KiB
Python
Raw Normal View History

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