server-start
This commit is contained in:
53
main.py
53
main.py
@@ -1,30 +1,47 @@
|
||||
from aiohttp import web
|
||||
from ariadne import make_executable_schema, load_schema_from_path
|
||||
import os
|
||||
from os.path import exists
|
||||
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 services.auth import JWTAuthenticate
|
||||
from services.redis import redis
|
||||
from resolvers import resolvers
|
||||
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, SESSION_SECRET_KEY, MODE
|
||||
|
||||
type_defs = load_schema_from_path("inbox.graphql")
|
||||
schema = make_executable_schema(type_defs, resolvers)
|
||||
schema = make_executable_schema(load_schema_from_path("schema.graphql"), resolvers) # type: ignore
|
||||
|
||||
middleware = [
|
||||
Middleware(AuthenticationMiddleware, backend=JWTAuthenticate()),
|
||||
Middleware(SessionMiddleware, secret_key=SESSION_SECRET_KEY),
|
||||
]
|
||||
|
||||
|
||||
async def on_startup(_app):
|
||||
await redis.connect()
|
||||
async def start_up():
|
||||
if MODE == "dev":
|
||||
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()
|
||||
try:
|
||||
import sentry_sdk
|
||||
|
||||
sentry_sdk.init(SENTRY_DSN)
|
||||
except Exception as e:
|
||||
print("[sentry] init error")
|
||||
print(e)
|
||||
|
||||
|
||||
async def on_cleanup(_app):
|
||||
async def shutdown():
|
||||
await redis.disconnect()
|
||||
|
||||
|
||||
# Run the aiohttp server
|
||||
if __name__ == "__main__":
|
||||
app = web.Application()
|
||||
app.on_startup.append(on_startup)
|
||||
app.on_cleanup.append(on_cleanup)
|
||||
app.router.add_route(
|
||||
"*",
|
||||
"/graphql",
|
||||
GraphQL(schema),
|
||||
)
|
||||
web.run_app(app)
|
||||
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown])
|
||||
app.mount("/", GraphQL(schema, debug=True))
|
||||
|
Reference in New Issue
Block a user