-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
100 lines (84 loc) · 2.94 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
import os
from flask import Flask, jsonify, render_template
from flask_smorest import Api
from db import db
from resources.journal import blp as JournalBlueprint
from resources.metadata import blp as MetadataBlueprint
from resources.category import blp as CategoryBlueprint
from resources.body import blp as BodyBlueprint
from resources.signature import blp as SignatureBlueprint
from resources.user import blp as UserBlueprint
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate
from blocklist import BLOCKLIST
from dotenv import load_dotenv
def create_app():
app = Flask(__name__)
@app.get("/")
def main():
return render_template("index.html")
load_dotenv()
app.config["PROPAGATE_EXCEPTIONS"] = True
app.config["API_TITLE"] = "Diário Oficial da União Web Restful API"
app.config["API_VERSION"] = "v4"
app.config["OPENAPI_VERSION"] = "3.0.3"
app.config["OPENAPI_URL_PREFIX"] = "/"
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
app.config[
"OPENAPI_SWAGGER_UI_URL"
] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/"
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL", "sqlite:///data.db")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
migrate = Migrate(app, db)
api = Api(app)
app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY")
jwt = JWTManager(app)
@jwt.token_in_blocklist_loader
def check_if_token_in_blocklist(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,
)
with app.app_context():
db.create_all()
api.register_blueprint(JournalBlueprint)
api.register_blueprint(MetadataBlueprint)
api.register_blueprint(CategoryBlueprint)
api.register_blueprint(BodyBlueprint)
api.register_blueprint(SignatureBlueprint)
api.register_blueprint(UserBlueprint)
return app