-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
70 lines (59 loc) · 2.15 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
import os
from datetime import timedelta
from dotenv import load_dotenv
from flask import Flask, jsonify
from flask_smorest import Api
from flask_jwt_extended import JWTManager
from db import db
import models
from models import UserModel
from resources.files import blp as FilesBlueprint
from resources.users import blp as UserBlueprint
from resources.super_admin import blp as SuperAdminBlueprint
from resources.admin import blp as AdminBluerprint
def create_app():
load_dotenv()
app = Flask(__name__)
app.config["API_SECRET_KEY"] = os.getenv("API_SECRET_KEY")
app.config["API_TITLE"] = "PROFILE"
app.config["API_VERSION"] = "v1"
app.config["OPENAPI_VERSION"] = "3.0.3"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
api = Api(app)
app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY")
jwt = JWTManager(app)
with app.app_context():
db.create_all()
@jwt.user_lookup_loader # GET CURRENT USER
def user_lookup_callback(jwt_header, jwt_payload):
username_uuid = jwt_payload["sub"]
return UserModel.query.filter_by(username_uuid=username_uuid).first()
@jwt.unauthorized_loader
def missing_token_callback(error):
return jsonify(
{
"description": "Request does not contain an access token",
"error": "authorizatiion_required",
}
)
@jwt.invalid_token_loader
def invalid_token_callbak(error):
return (
jsonify(
{"message": "Signature verification failed", "error": "invalid token"}
),
401,
)
@jwt.expired_token_loader
def expired_token_callback(jwt_header, jwt_payload):
return (
jsonify({"message": "The token has expired", "error": "token_expired"}),
401,
)
api.register_blueprint(FilesBlueprint)
api.register_blueprint(UserBlueprint)
api.register_blueprint(SuperAdminBlueprint)
api.register_blueprint(AdminBluerprint)
return app