From 07cedc9545f00fe41486c0020c081f121bb33b5c Mon Sep 17 00:00:00 2001 From: Gabriel Le Breton Date: Mon, 12 Jun 2017 23:45:37 -0400 Subject: [PATCH] Solves #17 when running with ENABLE_CORS=1 env variable * Uses [Flask-Cors](https://flask-cors.readthedocs.io/en/latest/) * Adds default config `ENABLE_CORS = False` + env var * Adds default config `CORS_ORIGINS = "*"` + env var :v: --- requestbin/__init__.py | 12 +++++++++--- requestbin/config.py | 3 +++ requirements.txt | 3 ++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/requestbin/__init__.py b/requestbin/__init__.py index d672a05..b7e978a 100644 --- a/requestbin/__init__.py +++ b/requestbin/__init__.py @@ -1,8 +1,11 @@ -from flask import Flask, redirect, url_for -import config, os - +import config +import os from cStringIO import StringIO +from flask import Flask +from flask_cors import CORS + + class WSGIRawBody(object): def __init__(self, application): self.application = application @@ -33,6 +36,9 @@ def callback(status, headers, exc_info=None): app = Flask(__name__) +if os.environ.get('ENABLE_CORS', config.ENABLE_CORS): + cors = CORS(app, resources={r"*": {"origins": os.environ.get('CORS_ORIGINS', config.CORS_ORIGINS)}}) + from werkzeug.contrib.fixers import ProxyFix app.wsgi_app = WSGIRawBody(ProxyFix(app.wsgi_app)) diff --git a/requestbin/config.py b/requestbin/config.py index 209f394..9238290 100644 --- a/requestbin/config.py +++ b/requestbin/config.py @@ -6,6 +6,9 @@ PORT_NUMBER = 4000 +ENABLE_CORS = False +CORS_ORIGINS = "*" + FLASK_SESSION_SECRET_KEY = os.environ.get("SESSION_SECRET_KEY", "N1BKhJLnBqLpexOZdklsfDKFJDKFadsfs9a3r324YB7B73AglRmrHMDQ9RhXz35") BIN_TTL = 48*3600 diff --git a/requirements.txt b/requirements.txt index c2d4efc..38f400f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,9 +7,10 @@ feedparser==5.1.3 Jinja2==2.7 Werkzeug==0.9.3 Flask==0.10.1 +Flask-Cors==3.0.2 redis==2.7.6 msgpack-python==0.1.12 python-dateutil==2.1 gunicorn bugsnag -blinker \ No newline at end of file +blinker