Commit 8f1dc3b0 authored by Dan Jones's avatar Dan Jones
Browse files

Merge branch '28-make-api-cors-settings-configurable-in-config-settings' into 'dev'

Resolve "Make API CORS settings configurable in config settings"

Closes #28

See merge request !14
2 merge requests!23Resolve "Release v0.1.0",!14Resolve "Make API CORS settings configurable in config settings"
Pipeline #108556 passed with stages
in 1 minute and 27 seconds
__pycache__/
data/clients.json
data/*.json
examples/
rmq.log
Pipfile
......
......@@ -80,6 +80,43 @@ In your virtual environment:
python api.py
```
##### Config
The API reads it's config from `./data/api-config.json` if present.
The default config for development environments only enables
connections from local host on any port `http://localhost:*`.
If you want to open the API up to connections from anywhere
or from specific known client domains you can create an
`api-config.json` to do that.
This can also be used to limit requests to specified endpoints.
The default config is intended for running a local development
environment. For production it is expected that the config file
will exist with settings like the following:
```json
{
"cors": {
"*": {
"origins": [
"*"
]
},
"/client": {
"origins": [
"http://localhost:*"
]
}
}
}
```
This opens up the all endpoints except for the `/client` endpoint.
For now we expect client administration to be managed centrally.
#### Event bus
In your virtual environment:
......
import json
import logging
import os
from flask import Flask
from flask_cors import CORS
from flask_restful import Api
......@@ -10,16 +13,31 @@ from endpoints.token import Token
from models.token_model import TokenModel
import os
logging.basicConfig(level=logging.INFO)
logging.getLogger("pika").setLevel(logging.ERROR)
token = TokenModel()
token.setSecret()
def get_config():
config = {"cors": {r"*": {"origins": "http://localhost:*"}}}
try:
with open("./data/api-config.json", "r") as config_file:
config = {
r"{}".format(key): value
for key, value in json.load(config_file).items()
}
except FileNotFoundError:
logging.info("No API config: Using default")
return config
def create_app():
app = Flask(__name__)
api = Api(app)
CORS(app, resources={r"*": {"origins": "http://localhost:8086"}})
api_config = get_config()
logging.info(str(api_config))
CORS(app, resources=api_config["cors"])
api.add_resource(ClientList, "/client")
api.add_resource(Client, "/client/<client_id>")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment