core/main.py

95 lines
2.7 KiB
Python
Raw Normal View History

2023-10-26 21:07:35 +00:00
import asyncio
import os
from importlib import import_module
from os.path import exists
2023-10-30 21:00:55 +00:00
2022-09-03 10:50:14 +00:00
from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL
2023-10-26 21:07:35 +00:00
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-09-03 10:50:14 +00:00
from auth.authenticate import JWTAuthenticate
2023-10-30 21:00:55 +00:00
from auth.oauth import oauth_authorize, oauth_login
2022-09-03 10:50:14 +00:00
from base.redis import redis
from base.resolvers import resolvers
2023-10-30 21:00:55 +00:00
from orm import init_tables
2023-07-13 13:01:58 +00:00
from resolvers.upload import upload_handler
from services.main import storages_init
from services.notifications.notification_service import notification_service
2023-10-30 21:00:55 +00:00
from services.notifications.sse import sse_subscribe_handler
2022-11-18 18:22:10 +00:00
from services.stat.viewed import ViewedStorage
2023-10-30 21:00:55 +00:00
# from services.zine.gittask import GitTask
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, SESSION_SECRET_KEY
2022-11-22 23:51:29 +00:00
2022-09-03 10:50:14 +00:00
import_module("resolvers")
2023-10-30 21:00:55 +00:00
schema = make_executable_schema(load_schema_from_path("schema.graphql"), resolvers)
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()
2022-11-16 09:23:32 +00:00
await storages_init()
2022-11-18 18:22:10 +00:00
views_stat_task = asyncio.create_task(ViewedStorage().worker())
2022-11-15 09:25:04 +00:00
print(views_stat_task)
# git_task = asyncio.create_task(GitTask.git_task_worker())
# print(git_task)
notification_service_task = asyncio.create_task(notification_service.worker())
print(notification_service_task)
2022-12-04 08:24:43 +00:00
try:
import sentry_sdk
2023-10-30 21:00:55 +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-30 21:00:55 +00:00
print("[sentry] init error")
2022-12-04 08:24:43 +00:00
print(e)
2022-09-03 10:50:14 +00:00
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-30 21:00:55 +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-10-30 21:00:55 +00:00
Route("/upload", endpoint=upload_handler, methods=["POST"]),
Route("/subscribe/{user_id}", endpoint=sse_subscribe_handler),
2022-09-03 10:50:14 +00:00
]
app = Starlette(
on_startup=[start_up],
on_shutdown=[shutdown],
middleware=middleware,
routes=routes,
)
2023-10-30 21:00:55 +00:00
app.mount("/", GraphQL(schema))
2022-11-22 23:51:29 +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-30 21:00:55 +00:00
dev_app.mount("/", GraphQL(schema, debug=True))