-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
46 lines (35 loc) · 1.42 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
from flask import Flask, jsonify
from flask_restful import Api
from controller.clientes import Clientes, Cliente
from controller.usuario import User, UserConfirm, UserRegister, UserLogin, UserLogout, UserConfirm
from flask_jwt_extended import JWTManager
from blacklist import BLACKLIST
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///banco.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['JWT_SECRET_KEY'] = 'segredo'
app.config['JWT_BLACKLIST_ENABLED'] = True
api = Api(app)
jwt = JWTManager(app)
# Criar o banco primeiro
@app.before_first_request
def criar_banco():
banco.create_all()
@jwt.token_in_blocklist_loader
def verifica_blacklist(self,token):
return token['jti'] in BLACKLIST
@jwt.revoked_token_loader
def token_de_acesso_invalidado(jwt_header, jwt_payload):
return jsonify({'resultado': 'Token de acesso inválido!'}), 401
# Criação das rotas
api.add_resource(Clientes, '/clientes')
api.add_resource(Cliente, '/clientes/<string:cliente_id>')
api.add_resource(User, '/usuarios/<int:user_id>')
api.add_resource(UserRegister, '/cadastro') # Criação de usuário
api.add_resource(UserLogin, '/login') # Login da conta
api.add_resource(UserLogout, '/logout') # Logout da conta
api.add_resource(UserConfirm, '/confirmacao/<int:user_id>') # Confirmação de cadastro
if __name__ == '__main__':
from sql_alchemy import banco
banco.init_app(app)
app.run(debug=True)