diff --git a/settings-example.json b/settings-example.json index d98e572dbc4dfe05b3bfaba7f88ddbbc59aa9495..9b584d27acd90de3d0fa6c31171f27e8e6c0b472 100644 --- a/settings-example.json +++ b/settings-example.json @@ -5,7 +5,10 @@ "secret": "", "useSSL": false, "keyfile": "", - "certfile": "" + "certfile": "", + "cors_origins": [ + "http://localhost:8080" + ] }, "database": { "databasePath": "storageLeaf.db", diff --git a/src/StorageLeaf.py b/src/StorageLeaf.py index e5493fc37c5509b8992c47359231b0a46aaa7355..5bda99cab00bba467f6a7bd8f535e1eefe844255 100644 --- a/src/StorageLeaf.py +++ b/src/StorageLeaf.py @@ -4,6 +4,7 @@ import os import uvicorn from TheCodeLabs_BaseUtils.DefaultLogger import DefaultLogger from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware from starlette.responses import RedirectResponse, FileResponse from Settings import SETTINGS @@ -29,6 +30,15 @@ app = FastAPI(title=Constants.APP_NAME, description='The StorageLeaf API', servers=[{'url': SETTINGS['api']['url'], 'description': f'{Constants.APP_NAME} API'}]) +if 'cors_origins' in SETTINGS['server']: + app.add_middleware( + CORSMiddleware, + allow_origins=SETTINGS['server']['cors_origins'], + allow_credentials=True, + allow_methods=['*'], + allow_headers=['*'], + ) + @app.get('/', include_in_schema=False) async def root():