Skip to content

Commit

Permalink
Merge pull request #247 from Boavizta/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
da-ekchajzer authored Nov 21, 2023
2 parents 43d434a + 3b14ed6 commit f880943
Showing 1 changed file with 22 additions and 3 deletions.
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

0 comments on commit f880943

Please sign in to comment.