-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
126 lines (111 loc) · 3.94 KB
/
Copy pathapp.py
File metadata and controls
126 lines (111 loc) · 3.94 KB
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
117
118
119
120
121
122
123
124
125
126
import os
from flask import Flask, jsonify
from dotenv import load_dotenv
from flask_smorest import Api
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate
from db import db
import models
from resources.administrator import blp as AdministratorBlueprint
from resources.users import blp as UserBlueprint
from resources.images import blp as ImagesBlueprint
from resources.videos import blp as VideosBlueprint
from resources.gallery import blp as GalleryBlueprint
from resources.super_user import blp as SuperUserBlueprint
from resources.age_gender import blp as AgeGenderBlueprint
import redis
from models_base import UserModel
from datetime import timedelta
from flask_cors import CORS
load_dotenv()
import redis
r = redis.Redis(
host="localhost",
port=6379,
password=os.getenv("REDIS_PASSWORD"),
decode_responses=True,
)
def create_app(db_url=None):
app = Flask(__name__)
CORS(app)
# CORS(app, resources={r"/": {"origins": "*"}})
# app.config['PROPAGATE_EXCEPTIONS'] = True
app.config["API_TITLE"] = "Stores REST API" # api needs this
app.config["API_VERSION"] = "v1" # api needs this
app.config["API_SECRET_KEY"] = os.getenv("API_SECRET_KEY")
app.config["OPENAPI_VERSION"] = "3.0.3" # api needs this
app.config["OPENAPI_URL_PREFIX"] = "/" # swagger needs this
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui" # swagger needs this
app.config[
"OPENAPI_SWAGGER_UI_URL"
] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/" # swagger needs this
app.config["SQLALCHEMY_DATABASE_URI"] = db_url or 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")
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(minutes=60)
jwt = JWTManager(app)
with app.app_context():
db.create_all()
# @jwt.token_in_blocklist_loader
# def check_if_token_in_blocklist(jwt_header, jwt_payload):
# blocked_tocken_list = r.lrange("BlockedToken", 0, -1)
# return jwt_payload["jti"] in blocked_tocken_list
@jwt.revoked_token_loader
def revoked_token_callback(jwt_header, jwt_payload):
return (
jsonify(
{"descrition": "The token has been revoked", "error": "token_revoked"}
),
401,
)
@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.",
}
),
401,
)
# @jwt.additional_claims_loader
# def add_claims_to_jwt(identity):
# if identity == 1:
# return {"is admin": True}
# return {"is admin": False}
@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_callbak(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",
}
)
api.register_blueprint(AdministratorBlueprint)
api.register_blueprint(UserBlueprint)
api.register_blueprint(ImagesBlueprint)
api.register_blueprint(VideosBlueprint)
api.register_blueprint(GalleryBlueprint)
api.register_blueprint(AgeGenderBlueprint)
api.register_blueprint(SuperUserBlueprint)
return app