Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #247

Merged
merged 6 commits into from
Nov 21, 2023
Merged

Dev #247

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions boaviztapi/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import json
import logging

import anyio
import markdown
import toml
from fastapi.middleware.cors import CORSMiddleware
import os
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from mangum import Mangum
from starlette.requests import Request
from starlette.responses import Response

from boaviztapi.routers import iot_router
from boaviztapi.routers.component_router import component_router
Expand All @@ -26,10 +30,26 @@
openapi_prefix = f"/{stage}" if stage else "/"
app = FastAPI(root_path=openapi_prefix) # Here is the magic
version = toml.loads(open(os.path.join(os.path.dirname(__file__), '../pyproject.toml'), 'r').read())['tool']['poetry']['version']

_logger = logging.getLogger(__name__)

origins = json.loads(os.getenv("ALLOWED_ORIGINS", '["*"]'))


# Ensure that even an uncaught exception includes CORS headers.
async def catch_exceptions_middleware(request: Request, call_next):
try:
return await call_next(request)
except Exception as e:
# ignore anyio's EndOfStream exception traceback which just clutters up logs
if isinstance(e.__context__, anyio.EndOfStream):
e.__suppress_context__ = True

_logger.exception(str(e), exc_info=e)
return Response('Internal Server Error', status_code=500)


app.middleware('http')(catch_exceptions_middleware)

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
Expand All @@ -46,11 +66,10 @@
app.include_router(consumption_profile)
app.include_router(utils_router)


if __name__ == '__main__':
import uvicorn

uvicorn.run('main:app', host='localhost', port=5000, reload=True, debug=True)
uvicorn.run('main:app', host='localhost', port=5000, reload=True)


@app.on_event("startup")
Expand Down