Skip to content

Commit

Permalink
Merge pull request #243 from cosmastech/middleware-exception
Browse files Browse the repository at this point in the history
Uncaught exception to include CORS middleware
  • Loading branch information
da-ekchajzer authored Nov 21, 2023
2 parents 6b9e34e + 422b294 commit 3b14ed6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<p align="center">
<img src="https://github.com/Boavizta/boaviztapi/blob/af7ca450518a2108f907736222a540908a258368/boavizta-logo-4.png" width="100">
<img src="https://github.com/Boavizta/boaviztapi/blob/main/boaviztapi_color.svg" height="100" alt="BoaviztAPI">
</p>
<h1 align="center">
Boavizta API
</h1>

An API to access [Boavizta's](https://boavizta.cmakers.io/) methodologies and impacts data [reference data](https://github.com/Boavizta/environmental-footprint-data).
<h3 align="center">
An API to access <a href="https://boavizta.cmakers.io/">Boavizta's</a> methodologies and data</a>
</h3>

---

See the [documentation](https://doc.api.boavizta.org/) for API usage and methodology.

Expand Down Expand Up @@ -37,15 +38,15 @@ The system follows a bottom-up approach in its development, organized into layer

## Run a local instance

## :whale: Run API using docker
### :whale: Run API using docker

```bash
$ docker run -p 5000:5000 ghcr.io/boavizta/boaviztapi:latest
```

Access API at http://localhost:5000

## Install using pip package
### Install using pip package

```bash
$ pip3 install boaviztapi
Expand Down
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
Binary file added boaviztapi_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions boaviztapi_color.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3b14ed6

Please sign in to comment.