felicity-lims/felicity/lims/boot.py

102 lines
3.3 KiB
Python
Raw Normal View History

from contextlib import asynccontextmanager
2024-01-28 21:17:16 +08:00
2023-11-22 17:13:16 +08:00
from fastapi import FastAPI
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
2024-01-24 23:10:44 +08:00
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
2023-11-22 21:52:18 +08:00
from starlette.middleware.cors import CORSMiddleware
from strawberry.extensions.tracing import OpenTelemetryExtension
2023-11-22 17:13:16 +08:00
from strawberry.fastapi import GraphQLRouter
2024-02-16 23:48:19 +08:00
from strawberry.subscriptions import (GRAPHQL_TRANSPORT_WS_PROTOCOL,
GRAPHQL_WS_PROTOCOL)
2023-09-11 13:02:05 +08:00
from felicity.api.deps import get_gql_context
from felicity.api.gql.schema import schema
from felicity.api.rest.api_v1 import api
from felicity.apps.common.channel import broadcast
from felicity.apps.events import observe_events
from felicity.apps.job.sched import felicity_workforce_init
from felicity.core import get_settings
2024-01-28 21:17:16 +08:00
from felicity.database.session import async_engine
from felicity.lims.seeds import initialize_felicity
from felicity.views import setup_webapp
2023-11-22 17:13:16 +08:00
settings = get_settings()
@asynccontextmanager
async def lifespan(app: FastAPI):
if settings.LOAD_SETUP_DATA:
2024-04-12 23:14:48 +08:00
await initialize_felicity()
felicity_workforce_init()
observe_events()
2024-08-02 14:18:25 +08:00
await broadcast.connect()
#
yield
2024-02-16 23:48:19 +08:00
#
2024-08-02 14:18:25 +08:00
await broadcast.disconnect()
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")
2024-01-28 21:17:16 +08:00
setup_webapp(app, settings.SERVE_WEBAPP, schema)
2023-09-11 13:02:05 +08:00
2023-11-22 17:13:16 +08:00
def register_graphql(app: FastAPI):
if settings.RUN_OPEN_TRACING:
schema.extensions = schema.extensions + (OpenTelemetryExtension,)
2024-01-28 21:17:16 +08:00
2023-11-22 21:52:18 +08:00
graphql_app = GraphQLRouter(
schema,
graphiql=True,
context_getter=get_gql_context,
2024-02-16 23:48:19 +08:00
subscription_protocols=[GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL],
2023-11-22 21:52:18 +08:00
)
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
def register_tracer(app: FastAPI):
2024-01-24 23:10:44 +08:00
if settings.RUN_OPEN_TRACING:
2024-02-16 23:48:19 +08:00
otlp_exporter = OTLPSpanExporter(
endpoint=settings.OTLP_SPAN_EXPORT_URL, insecure=True
)
resource = Resource.create({"service.name": settings.PROJECT_NAME})
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)
span_processor = SimpleSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
#
FastAPIInstrumentor.instrument_app(app)
SQLAlchemyInstrumentor().instrument(
2024-02-16 23:48:19 +08:00
engine=async_engine.sync_engine, enable_commenter=True, commenter_options={}
)
2024-01-24 23:10:44 +08:00
def factory(config: dict):
config["lifespan"] = lifespan
app = FastAPI(**config)
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)
register_tracer(app)
return app