-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (51 loc) · 1.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
from flask import Flask, jsonify, make_response
from flask_restful import Api
from werkzeug.exceptions import HTTPException
from werkzeug.exceptions import default_exceptions
from dotenv import load_dotenv
from config import APP_CONFIG
from endpoints import RESOURCES
from utils.start_up import init_redis
load_dotenv()
def create_app(config_mode):
app = Flask(__name__)
# pylint: disable=W0612
@app.after_request
def af_request(resp):
resp = make_response(resp)
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE,OPTIONS"
resp.headers["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type, Authorization"
return resp
# pylint: enable=W0612
@app.errorhandler(Exception)
def handle_error(error):
code = 500
if isinstance(error, HTTPException):
code = error.code
# pylint: disable=E1101
app.logger.warning(f"{code} - {error}")
# pylint: enable=E1101
return jsonify(error=str(error)), code
for ex in default_exceptions:
app.register_error_handler(ex, handle_error)
# flask config
app.config.from_object(APP_CONFIG[config_mode])
# pylint: disable=E1101
app.logger.info(f"APP Mode: {config_mode}")
# pylint: enable=E1101
# Route Init
api = Api(app)
for resource in RESOURCES:
api.add_resource(RESOURCES[resource], f"/{resource}")
# run when app start
with app.app_context():
init_redis()
return app
def main():
config_name = os.environ.get("APP_SETTINGS", "development")
app = create_app(config_name)
app.run(host=app.config["HOST"], port=app.config["PORT"], threaded=True)
if __name__ == "__main__":
main()