Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Sep 2, 2019
1 parent 254efc8 commit de54374
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions 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
Expand All @@ -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():
Expand Down
6 changes: 4 additions & 2 deletions project_amber/config.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Binary file modified requirements.txt
Binary file not shown.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -24,6 +24,7 @@
packages=["project_amber"],
install_requires=[
"flask",
"flask-cors",
"flask-sqlalchemy",
"bcrypt"
],
Expand Down

0 comments on commit de54374

Please sign in to comment.