2023-11-22 17:13:16 +08:00
|
|
|
from fastapi import FastAPI
|
2024-01-24 23:10:44 +08:00
|
|
|
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
2023-11-22 21:52:18 +08:00
|
|
|
from starlette.middleware.cors import CORSMiddleware
|
2023-11-22 17:13:16 +08:00
|
|
|
from strawberry.fastapi import GraphQLRouter
|
2023-11-22 21:52:18 +08:00
|
|
|
from strawberry.subscriptions import GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-11-22 21:52:18 +08:00
|
|
|
from api.deps import get_gql_context
|
2023-08-14 02:08:08 +08:00
|
|
|
from api.gql.schema import schema
|
|
|
|
from api.rest.api_v1 import api
|
2023-12-30 15:51:11 +08:00
|
|
|
from apps.events import observe_events
|
2023-08-26 04:40:56 +08:00
|
|
|
from apps.job.sched import felicity_workforce_init
|
2023-08-14 02:08:08 +08:00
|
|
|
from core import settings
|
2023-11-22 17:13:16 +08:00
|
|
|
from init import initialize_felicity
|
|
|
|
from views import setup_webapp
|
|
|
|
|
|
|
|
|
2023-11-22 21:52:18 +08:00
|
|
|
def register_cors(app: FastAPI):
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
def register_routes(app: FastAPI):
|
|
|
|
@app.get("/health")
|
|
|
|
async def get_health(request):
|
|
|
|
return {"up": True}
|
2023-08-14 02:08:08 +08:00
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
app.include_router(api, prefix="/api/v1")
|
|
|
|
setup_webapp(app, settings.SERVE_WEBAPP)
|
2023-08-14 02:08:08 +08:00
|
|
|
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
def register_listeners(app: FastAPI):
|
|
|
|
@app.on_event("startup")
|
|
|
|
async def init_seeds():
|
|
|
|
if settings.LOAD_SETUP_DATA:
|
|
|
|
await initialize_felicity()
|
2023-08-26 04:53:40 +08:00
|
|
|
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
def register_graphql(app: FastAPI):
|
2023-11-22 21:52:18 +08:00
|
|
|
graphql_app = GraphQLRouter(
|
|
|
|
schema,
|
|
|
|
graphiql=True,
|
|
|
|
context_getter=get_gql_context,
|
|
|
|
subscription_protocols=[GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL]
|
|
|
|
# GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL
|
|
|
|
)
|
2023-11-22 17:13:16 +08:00
|
|
|
app.include_router(graphql_app, prefix="/felicity-gql")
|
2023-11-22 21:52:18 +08:00
|
|
|
app.add_websocket_route("/felicity-gql", graphql_app)
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-08-14 02:08:08 +08:00
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
def register_tasks(app: FastAPI):
|
|
|
|
# bg_tasks = BackgroundTasks(tasks=None)
|
|
|
|
# bg_tasks.add_task(felicity_workforce_init)
|
|
|
|
felicity_workforce_init()
|
2023-12-30 15:51:11 +08:00
|
|
|
observe_events()
|
2023-08-14 02:08:08 +08:00
|
|
|
|
|
|
|
|
2024-01-24 23:10:44 +08:00
|
|
|
def trace_app(app: FastAPI):
|
|
|
|
if settings.RUN_OPEN_TRACING:
|
|
|
|
FastAPIInstrumentor.instrument_app(app)
|
|
|
|
|
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
def register_felicity(app: FastAPI):
|
2023-11-22 21:52:18 +08:00
|
|
|
register_cors(app)
|
2023-11-22 17:13:16 +08:00
|
|
|
register_routes(app)
|
2023-08-14 02:08:08 +08:00
|
|
|
register_graphql(app)
|
2023-11-22 17:13:16 +08:00
|
|
|
register_listeners(app)
|
|
|
|
register_tasks(app)
|
2024-01-24 23:10:44 +08:00
|
|
|
trace_app(app)
|