-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
58 lines (41 loc) · 1.48 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
from flask import Flask, request
import os
import sys
import click
from werkzeug.exceptions import HTTPException
import traceback
from dotenv import load_dotenv
load_dotenv()
def create_app():
sys.path.append(".") # to allow sub modules to access the parent module easily
from db.shared import db
from api import api as api_blueprint
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"DB_PATH", "sqlite:///database.db"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config['JSON_SORT_KEYS'] = False
db.init_app(app)
app.register_blueprint(api_blueprint, url_prefix="/api")
@app.errorhandler(404)
def handle_bad_request(e):
return {"error": "The route is not defined"}, 404
@app.errorhandler(Exception)
def handle_exception(e):
# pass through HTTP errors. You wouldn't want to handle these generically.
if isinstance(e, HTTPException):
return e
# now you're handling non-HTTP exceptions only
return {"message": repr(e), "stack": traceback.format_exc()}, 500
@app.cli.command()
@click.argument("test_names", nargs=-1)
def test(test_names):
"""Run the unit tests."""
import pytest
if test_names:
sys.exit(pytest.main(["-vv", *test_names]))
else:
sys.exit(pytest.main(["-vv", "tests/"]))
return app
app = create_app()