♻️ Use of pydantic to manage config+env variables
FastAPI release has been updated allowing to use lifespan parameter to prepare/shutdown sub components.
This commit is contained in:
106
backend/main.py
106
backend/main.py
@@ -6,21 +6,18 @@ from os import environ, EX_USAGE
|
||||
from typing import Sequence
|
||||
|
||||
import uvicorn
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from opentelemetry import trace
|
||||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
||||
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
||||
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
|
||||
from opentelemetry.sdk.trace import TracerProvider
|
||||
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
||||
|
||||
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
||||
from opentelemetry.instrumentation.logging import LoggingInstrumentor
|
||||
from opentelemetry.sdk.resources import Resource as OtResource
|
||||
from opentelemetry.sdk.trace import TracerProvider as OtTracerProvider
|
||||
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
||||
from rich import print
|
||||
from starlette.types import ASGIApp
|
||||
from yaml import safe_load
|
||||
|
||||
from backend.db import db
|
||||
from backend.idfm_interface import Destinations as IdfmDestinations, IdfmInterface
|
||||
@@ -34,65 +31,54 @@ from backend.schemas import (
|
||||
StopArea as StopAreaSchema,
|
||||
StopShape as StopShapeSchema,
|
||||
)
|
||||
|
||||
API_KEY = environ.get("API_KEY")
|
||||
if API_KEY is None:
|
||||
print('No "API_KEY" environment variable set... abort.')
|
||||
exit(EX_USAGE)
|
||||
|
||||
APP_NAME = environ.get("APP_NAME", "app")
|
||||
MODE = environ.get("MODE", "grpc")
|
||||
COLLECTOR_ENDPOINT_GRPC_ENDPOINT = environ.get(
|
||||
"COLLECTOR_ENDPOINT_GRPC_ENDPOINT", "127.0.0.1:14250" # "jaeger-collector:14250"
|
||||
)
|
||||
|
||||
# CREATE DATABASE "carrramba-encore-rate";
|
||||
# CREATE USER cer WITH ENCRYPTED PASSWORD 'cer_password';
|
||||
# GRANT ALL PRIVILEGES ON DATABASE "carrramba-encore-rate" TO cer;
|
||||
# \c "carrramba-encore-rate";
|
||||
# GRANT ALL ON schema public TO cer;
|
||||
# CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
||||
|
||||
# TODO: Remove postgresql+psycopg from environ variable
|
||||
DB_PATH = "postgresql+psycopg://cer:cer_password@127.0.0.1:5432/carrramba-encore-rate"
|
||||
from backend.settings import Settings
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
CONFIG_PATH = environ.get("CONFIG_PATH", "./config.sample.yaml")
|
||||
|
||||
|
||||
def load_settings(path: str) -> Settings:
|
||||
with open(path, "r") as config_file:
|
||||
config = safe_load(config_file)
|
||||
|
||||
return Settings(**config)
|
||||
|
||||
|
||||
settings = load_settings(CONFIG_PATH)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
await db.connect(settings.db, settings.clear_static_data)
|
||||
if settings.clear_static_data:
|
||||
await idfm_interface.startup()
|
||||
|
||||
yield
|
||||
|
||||
await db.disconnect()
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[
|
||||
"https://localhost:4443",
|
||||
"https://localhost:3000",
|
||||
],
|
||||
allow_origins=["https://localhost:4443", "https://localhost:3000"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
trace.set_tracer_provider(TracerProvider())
|
||||
app.mount("/widget", StaticFiles(directory="../frontend/", html=True), name="widget")
|
||||
|
||||
FastAPIInstrumentor.instrument_app(app)
|
||||
|
||||
trace.set_tracer_provider(
|
||||
TracerProvider(resource=Resource.create({SERVICE_NAME: settings.app_name}))
|
||||
)
|
||||
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
|
||||
tracer = trace.get_tracer(APP_NAME)
|
||||
tracer = trace.get_tracer(settings.app_name)
|
||||
|
||||
|
||||
idfm_interface = IdfmInterface(API_KEY, db)
|
||||
|
||||
|
||||
# TODO: Add command line argument to force database reset.
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
await db.connect(DB_PATH, clear_static_data=True)
|
||||
await idfm_interface.startup()
|
||||
# await db.connect(DB_PATH, clear_static_data=False)
|
||||
print("Connected")
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
await db.disconnect()
|
||||
|
||||
|
||||
STATIC_ROOT = "../frontend/"
|
||||
app.mount("/widget", StaticFiles(directory=STATIC_ROOT, html=True), name="widget")
|
||||
idfm_interface = IdfmInterface(settings.idfm_api_key.get_secret_value(), db)
|
||||
|
||||
|
||||
def optional_datetime_to_ts(dt: datetime | None) -> int | None:
|
||||
@@ -281,7 +267,11 @@ async def get_stop_shape(stop_id: int) -> StopShapeSchema | None:
|
||||
raise HTTPException(status_code=404, detail=msg)
|
||||
|
||||
|
||||
FastAPIInstrumentor.instrument_app(app)
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=4443, ssl_certfile="./config/cert.pem")
|
||||
http_settings = settings.http
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=http_settings.host,
|
||||
port=http_settings.port,
|
||||
ssl_certfile=http_settings.cert,
|
||||
)
|
||||
|
Reference in New Issue
Block a user