|
5 | 5 | from flask import Flask, request, jsonify, url_for, send_from_directory
|
6 | 6 | from flask_migrate import Migrate
|
7 | 7 | from flask_swagger import swagger
|
8 |
| -from flask_cors import CORS |
9 | 8 | from api.utils import APIException, generate_sitemap
|
10 | 9 | from api.models import db
|
11 | 10 | from api.routes import api
|
12 | 11 | from api.admin import setup_admin
|
13 | 12 | from api.commands import setup_commands
|
14 | 13 |
|
15 |
| -#from models import Person |
| 14 | +# from models import Person |
16 | 15 |
|
17 | 16 | ENV = "development" if os.getenv("FLASK_DEBUG") == "1" else "production"
|
18 |
| -static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../public/') |
| 17 | +static_file_dir = os.path.join(os.path.dirname( |
| 18 | + os.path.realpath(__file__)), '../public/') |
19 | 19 | app = Flask(__name__)
|
20 | 20 | app.url_map.strict_slashes = False
|
21 | 21 |
|
22 | 22 | # database condiguration
|
23 | 23 | db_url = os.getenv("DATABASE_URL")
|
24 | 24 | if db_url is not None:
|
25 |
| - app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace("postgres://", "postgresql://") |
| 25 | + app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace( |
| 26 | + "postgres://", "postgresql://") |
26 | 27 | else:
|
27 | 28 | app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////tmp/test.db"
|
28 | 29 |
|
29 | 30 | app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
30 |
| -MIGRATE = Migrate(app, db, compare_type = True) |
| 31 | +MIGRATE = Migrate(app, db, compare_type=True) |
31 | 32 | db.init_app(app)
|
32 | 33 |
|
33 |
| -# Allow CORS requests to this API |
34 |
| -CORS(app) |
35 |
| - |
36 | 34 | # add the admin
|
37 | 35 | setup_admin(app)
|
38 | 36 |
|
|
43 | 41 | app.register_blueprint(api, url_prefix='/api')
|
44 | 42 |
|
45 | 43 | # Handle/serialize errors like a JSON object
|
| 44 | + |
| 45 | + |
46 | 46 | @app.errorhandler(APIException)
|
47 | 47 | def handle_invalid_usage(error):
|
48 | 48 | return jsonify(error.to_dict()), error.status_code
|
49 | 49 |
|
50 | 50 | # generate sitemap with all your endpoints
|
| 51 | + |
| 52 | + |
51 | 53 | @app.route('/')
|
52 | 54 | def sitemap():
|
53 | 55 | if ENV == "development":
|
54 | 56 | return generate_sitemap(app)
|
55 | 57 | return send_from_directory(static_file_dir, 'index.html')
|
56 | 58 |
|
57 | 59 | # any other endpoint will try to serve it like a static file
|
| 60 | + |
| 61 | + |
58 | 62 | @app.route('/<path:path>', methods=['GET'])
|
59 | 63 | def serve_any_other_file(path):
|
60 | 64 | if not os.path.isfile(os.path.join(static_file_dir, path)):
|
61 | 65 | path = 'index.html'
|
62 | 66 | response = send_from_directory(static_file_dir, path)
|
63 |
| - response.cache_control.max_age = 0 # avoid cache memory |
| 67 | + response.cache_control.max_age = 0 # avoid cache memory |
64 | 68 | return response
|
65 | 69 |
|
66 | 70 |
|
|
0 commit comments