From 2d16c68b1588e8f3dcd063bc294a1f07b3f88f65 Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Sat, 13 Feb 2021 17:27:29 +0100
Subject: [PATCH] Add cors settings

---
 settings-example.json |  5 ++++-
 src/StorageLeaf.py    | 10 ++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/settings-example.json b/settings-example.json
index d98e572..9b584d2 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 e5493fc..5bda99c 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():
-- 
GitLab