api.py 1.51 KB
Newer Older
1 2 3
import json
import logging
import os
4
from flask import Flask
James Kirk's avatar
James Kirk committed
5
from flask_cors import CORS
6
from flask_restful import Api
James Kirk's avatar
James Kirk committed
7

8
from endpoints.client import Client, ClientList
James Kirk's avatar
James Kirk committed
9
from endpoints.notify import Notify
10
from endpoints.receive import Receive
11
from endpoints.send import Send
12
from endpoints.token import Token
13 14
from models.token_model import TokenModel

15

16 17
logging.basicConfig(level=logging.INFO)
logging.getLogger("pika").setLevel(logging.ERROR)
18 19
token = TokenModel()
token.setSecret()
20 21


22 23 24 25 26 27 28 29 30 31 32 33 34
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


35 36 37
def create_app():
    app = Flask(__name__)
    api = Api(app)
38 39 40
    api_config = get_config()
    logging.info(str(api_config))
    CORS(app, resources=api_config["cors"])
41 42 43 44 45 46 47 48 49

    api.add_resource(ClientList, "/client")
    api.add_resource(Client, "/client/<client_id>")
    api.add_resource(Receive, "/receive")
    api.add_resource(Send, "/send")
    api.add_resource(Notify, "/notify")
    api.add_resource(Token, "/token")
    return app

50

51 52 53
flask_host = os.getenv(
    "FLASK_HOST", "localhost"
)  # Sets to whatever MQ_HOST is, or defaults to localhost
54

55
if __name__ == "__main__":
56
    app = create_app()
57
    app.run(debug=False, port=8087, host=flask_host)