-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathapp.py
69 lines (50 loc) · 1.82 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
"""A Python Flask REST API BoilerPlate (CRUD) Style"""
import argparse
import os
from flask import Flask, jsonify, make_response
from flask_cors import CORS
from flask_swagger_ui import get_swaggerui_blueprint
from routes import request_api
APP = Flask(__name__)
### swagger specific ###
SWAGGER_URL = '/swagger'
API_URL = '/static/swagger.json'
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Seans-Python-Flask-REST-Boilerplate"
}
)
APP.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
### end swagger specific ###
APP.register_blueprint(request_api.get_blueprint())
@APP.errorhandler(400)
def handle_400_error(_error):
"""Return a http 400 error to client"""
return make_response(jsonify({'error': 'Misunderstood'}), 400)
@APP.errorhandler(401)
def handle_401_error(_error):
"""Return a http 401 error to client"""
return make_response(jsonify({'error': 'Unauthorised'}), 401)
@APP.errorhandler(404)
def handle_404_error(_error):
"""Return a http 404 error to client"""
return make_response(jsonify({'error': 'Not found'}), 404)
@APP.errorhandler(500)
def handle_500_error(_error):
"""Return a http 500 error to client"""
return make_response(jsonify({'error': 'Server error'}), 500)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(
description="Seans-Python-Flask-REST-Boilerplate")
PARSER.add_argument('--debug', action='store_true',
help="Use flask debug/dev mode with file change reloading")
ARGS = PARSER.parse_args()
PORT = int(os.environ.get('PORT', 5000))
if ARGS.debug:
print("Running in debug mode")
CORS = CORS(APP)
APP.run(host='0.0.0.0', port=PORT, debug=True)
else:
APP.run(host='0.0.0.0', port=PORT, debug=False)