From de5437419d80368eebc8f85010655fcd47b5d7f6 Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Mon, 2 Sep 2019 23:32:29 +0500 Subject: [PATCH] Add CORS support --- README.md | 13 ++++++++----- project_amber/app.py | 2 ++ project_amber/config.py | 6 ++++-- requirements.txt | Bin 682 -> 748 bytes setup.py | 1 + 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ef8b5c0..3264fc4 100644 --- a/README.md +++ b/README.md @@ -15,17 +15,20 @@ Example config: { "database": "sqlite:///file.db", // SQLAlchemy database URI // see https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls - "allow_signup": false // whether to allow /api/signup or not - "loglevel": 0 // 0: errors, 1: warnings, 2: info + "allow_signup": false, // whether to allow /api/signup or not + "loglevel": 0, // 0: errors, 1: warnings, 2: info + "domain": "https://your.domain.tld" // full domain with HTTPS + // needed for CORS } If there are environment variables `AMBER_DATABASE` / `AMBER_ALLOW_SIGNUP` / -`AMBER_LOGLEVEL` set, the program will respect them and use over the values -provided with the config file. +`AMBER_LOGLEVEL`, `AMBER_DOMAIN` set, the program will respect them and use +over the values provided with the config file. #### Dependencies -This app directly depends on `flask`, `flask-sqlalchemy`, and `bcrypt`. +This app directly depends on `flask`, `flask-sqlalchemy`, `flask-cors`, and +`bcrypt`. #### Licenses diff --git a/project_amber/app.py b/project_amber/app.py index caded1c..c37db6a 100644 --- a/project_amber/app.py +++ b/project_amber/app.py @@ -1,6 +1,7 @@ from json import dumps from flask import Flask, request +from flask_cors import CORS from project_amber.config import config from project_amber.db import db @@ -16,6 +17,7 @@ app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = config["database"] db.init_app(app) +CORS(app, resources={r"/*": {"origins": config["domain"]}}) @app.before_request def middleware(): diff --git a/project_amber/config.py b/project_amber/config.py index cbd9e67..80f68aa 100644 --- a/project_amber/config.py +++ b/project_amber/config.py @@ -9,7 +9,8 @@ config = { "database": "", "loglevel": 0, - "allow_signup": False + "allow_signup": False, + "domain": "*" } # search for every file name and load the config from the first file @@ -55,7 +56,8 @@ def string_to_bool(val: str) -> bool: ("AMBER_DATABASE", "database", lambda val: val), # str -> str # pylint: disable=unnecessary-lambda ("AMBER_LOGLEVEL", "loglevel", lambda val: int(val)), # str -> int - ("AMBER_ALLOW_SIGNUP", "allow_signup", string_to_bool) # str -> bool + ("AMBER_ALLOW_SIGNUP", "allow_signup", string_to_bool), # str -> bool + ("AMBER_DOMAIN", "domain", lambda val: val) # str -> str ): env_value = os.getenv(mapping[0]) if not env_value is None: diff --git a/requirements.txt b/requirements.txt index e9754cce9f60e4878d738e2b48b731a9f08bcc55..c8ef1d2493a5473cc87685acae9afc1abdd41617 100644 GIT binary patch delta 61 zcmZ3*`i6DFK2c|ee1;;1Vg_3vG-l9aFaTnUjcflh%2YBWG88bRfn`9VMhq5Ud0qxC F1^~#{3+n&? delta 10 ScmaFEx{7tezKu`bFaiJ_K?QLD diff --git a/setup.py b/setup.py index b62ba82..b61256f 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ packages=["project_amber"], install_requires=[ "flask", + "flask-cors", "flask-sqlalchemy", "bcrypt" ],