-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
116 lines (88 loc) · 2.87 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import redis
from flask import Flask, jsonify
from flask_smorest import Api
from config.config import Config
from resources.store import blp as StoreBlueprint
from resources.item import blp as ItemBlueprint
from resources.tag import blp as TagBlueprint
from resources.user import blp as UserBlueprint
from db import db
from blocklist import BLOCKLIST
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate
from dotenv import load_dotenv
from rq import Queue
def create_app(db_url=None):
app = Flask(__name__)
load_dotenv()
_configure_app(app=app, db_url=db_url)
_create_databases(app=app)
_configure_redis(app=app)
_configure_migration(app=app, db=db)
_register_blueprints(app=app)
_configure_jwt(app=app)
return app
def _configure_app(app, db_url):
app.config.from_object(Config(db_url=db_url))
def _register_blueprints(app):
api = Api(app)
api.register_blueprint(ItemBlueprint)
api.register_blueprint(StoreBlueprint)
api.register_blueprint(TagBlueprint)
api.register_blueprint(UserBlueprint)
def _create_databases(app):
db.init_app(app)
with app.app_context():
db.create_all()
def _configure_redis(app):
connection = redis.from_url(os.getenv("REDIS_URL"))
app.queue = Queue("emails", connection=connection)
def _configure_migration(app, db):
migrate = Migrate(app, db=db)
def _configure_jwt(app):
jwt = JWTManager(app)
@jwt.needs_fresh_token_loader
def token_not_fresh_callback(jwt_header, jwt_payload):
return jsonify(
{"description": "The token is not fresh.", "error": "fresh_token_required"}
)
@jwt.token_in_blocklist_loader
def check_if_token_in_block_list(jwt_header, jwt_payload):
return jwt_payload["jti"] in BLOCKLIST
@jwt.revoked_token_loader
def revoked_token_callback(jwt_header, jwt_payload):
return (
jsonify(
{"description": "The token has been revoked.", "error": "token revoked"}
),
401,
)
@jwt.expired_token_loader
def expired_token_callback(jwt_header, jwt_payload):
return (
jsonify({"message": "The token has expired.", "error": "token expired"}),
401,
)
@jwt.invalid_token_loader
def invalid_token_callback(error):
return (
jsonify(
{"message": "Signature verification failed.", "error": "invalid token"}
),
401,
)
@jwt.unauthorized_loader
def missing_token_callback(error):
return (
jsonify(
{
"description": "Request does not contain an access token",
"error": "authorization required",
}
),
401,
)
# if __name__ == "__main__":
# app = create_app()
# app.run(debug=True)