balance-application/app.py

54 lines
1.3 KiB
Python
Raw Normal View History

2024-09-13 05:28:09 +00:00
import psycopg2
from generate import on_load
2024-09-10 04:14:09 +00:00
from fastapi import FastAPI, Response
2024-09-10 05:20:11 +00:00
from routes.auth import router as auth
2024-09-13 05:28:09 +00:00
from contextlib import asynccontextmanager
2024-10-10 06:22:13 +00:00
from util.config import conn_param, db_url, get_otld
2024-09-10 05:20:11 +00:00
from routes.balance import router as balance
2024-09-10 12:20:03 +00:00
from fastapi.middleware.cors import CORSMiddleware
2024-09-10 01:31:17 +00:00
2024-09-13 05:28:09 +00:00
@asynccontextmanager
async def lifespan(app: FastAPI):
conn = psycopg2.connect(conn_param)
cur = conn.cursor()
try:
print("loading database for: %s" % db_url())
on_load(conn, cur)
except:
print("[warn] error occurred while creating table. aborted")
finally:
cur.close()
conn.close()
yield
app = FastAPI(lifespan=lifespan)
2024-09-10 01:31:17 +00:00
2024-09-10 12:20:03 +00:00
app.add_middleware(
CORSMiddleware,
2024-09-10 12:23:22 +00:00
allow_origins=["*"],
2024-09-10 12:22:01 +00:00
allow_credentials=True,
2024-09-10 16:42:29 +00:00
allow_methods=["*"],
2024-09-10 12:20:03 +00:00
allow_headers=["*"]
)
2024-09-10 04:14:09 +00:00
@app.get("/")
2024-09-13 05:28:09 +00:00
async def index(resp: Response):
2024-09-10 04:14:09 +00:00
resp.headers.setdefault("Content-Type", "text")
2024-09-10 01:31:17 +00:00
return "Hello, World!"
2024-09-10 05:20:11 +00:00
app.include_router(router=auth)
app.include_router(router=balance)
2024-10-10 06:22:13 +00:00
if get_otld():
import logging
2024-10-10 06:33:57 +00:00
from otlp_tracing import configure_oltp_grpc_tracing
2024-10-10 06:22:13 +00:00
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
logging.basicConfig(level=logging.INFO)
2024-10-10 06:33:57 +00:00
tracer = configure_oltp_grpc_tracing()
2024-10-10 06:22:13 +00:00
logger = logging.getLogger(__name__)
FastAPIInstrumentor.instrument_app(app)