Skip to content

Commit d252224

Browse files
authored
Merge pull request #61 from ioet/feature/enable-cors#54
Enable CORS close #61
2 parents 3791b89 + ed1c301 commit d252224

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

requirements/prod.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ flask_sqlalchemy==2.4.1
3131
requests==2.23.0
3232

3333
# The Debug Toolbar
34-
Flask-DebugToolbar==0.11.0
34+
Flask-DebugToolbar==0.11.0
35+
36+
#CORS
37+
flask-cors==3.0.8

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33
setup(
4-
name="time-tracker-api",
4+
name="time-tracker-backend",
55
packages=find_packages(),
66
include_package_data=True,
77
)

time_tracker_api/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ def init_app(app: Flask):
4545
app.logger.setLevel(logging.INFO)
4646
add_debug_toolbar(app)
4747

48+
cors_origins = app.config.get('CORS_ORIGINS')
49+
if cors_origins:
50+
enable_cors(app, cors_origins)
4851

49-
def add_debug_toolbar(app):
52+
53+
def add_debug_toolbar(app: Flask):
5054
app.config['DEBUG_TB_PANELS'] = (
5155
'flask_debugtoolbar.panels.versions.VersionDebugPanel',
5256
'flask_debugtoolbar.panels.timer.TimerDebugPanel',
@@ -62,3 +66,10 @@ def add_debug_toolbar(app):
6266
from flask_debugtoolbar import DebugToolbarExtension
6367
toolbar = DebugToolbarExtension()
6468
toolbar.init_app(app)
69+
70+
71+
def enable_cors(app: Flask, cors_origins: str):
72+
from flask_cors import CORS
73+
cors_origins_list = cors_origins.split(",")
74+
CORS(app, resources={r"/*": {"origins": cors_origins_list}})
75+
app.logger.info("Set CORS access to [%s]" % cors_origins)

time_tracker_api/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Config:
1111
PROPAGATE_EXCEPTIONS = True
1212
RESTPLUS_VALIDATE = True
1313
DEBUG = True
14+
CORS_ORIGINS = "*"
1415

1516

1617
class DevelopmentConfig(Config):

0 commit comments

Comments
 (0)