-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
66 lines (51 loc) · 1.98 KB
/
api.py
File metadata and controls
66 lines (51 loc) · 1.98 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
import json
from flask import Flask, jsonify, request, abort, make_response
from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy_session import flask_scoped_session
from flask_jwt_extended import create_access_token, create_refresh_token,JWTManager,get_jwt_identity,jwt_required
from datetime import datetime
from flask_jsonschema_validator import JSONSchemaValidator
import jsonschema
UPLOAD_FOLDER = './images/requests'
DOMAIN_NAME = "UR_DOMAIN_ADDRESS"
IMAGE_FOLDER = './img'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@localhost/hackaton'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['JWT_SECRET_KEY'] = 'gfj454jklgfdkltjlrd554ggg'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000
JSONSchemaValidator( app = app, root = "schemas" )
engine_options = {
"max_overflow": 30,
"pool_pre_ping": True,
"pool_recycle": 3600,
"pool_size": 30,
'connect_args': { 'connect_timeout': 3700 }
}
jwt = JWTManager(app)
db = SQLAlchemy(app,engine_options=engine_options)
#run_with_ngrok(app)
from routes import *
app.register_blueprint(routes)
class TokenBlocklist(db.Model):
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
jti = db.Column(db.String(36), nullable=False)
created_at = db.Column(db.DateTime, nullable=False)
def __repr__(self):
return f"<regions {self.id}>"
# Callback function to check if a JWT exists in the database blocklist
@jwt.token_in_blocklist_loader
def check_if_token_revoked(jwt_header, jwt_payload):
jti = jwt_payload["jti"]
token = db.session.query(TokenBlocklist.id).filter_by(jti=jti).scalar()
return token is not None
@app.teardown_appcontext
def shutdown_session(exception=None):
db.session.remove()
@app.errorhandler( jsonschema.ValidationError )
def onValidationError( e ):
return jsonify(errors_response.incorrect_data),400
if __name__ == '__main__':
app.run()