def create_app(): # jwt配置 app.config.update(JWT_CONFIG) jwt = JWT(app, authenticate, identity) jwt.jwt_error_handler(jwt_error_handler) # 注册blueprint app.register_blueprint(bp_user_control) @app.after_request def after_request(resp): resp.headers['Access-Control-Allow-Origin'] = '*' resp.headers['Access-Control-Allow-Methods'] = '*' resp.headers['Access-Control-Allow-Headers'] = '*' resp.headers['Content-Type'] = 'application/json;charset=UTF-8' return resp return app
# Resources for routes from resources.items import Item, ItemList from resources.user import UserRegister # Applications should initialise Colorama init(autoreset=True) # Configure application app = Flask(__name__) api = Api(app) app.secret_key = config.APP_KEY # Configure JWT jwt = JWT(app, auth, indentity) jwt.jwt_error_handler(security_error_handler) # Configure DataBase DataBase.create_user_table(config.USER_DB_PATH, new=True) DataBase.create_item_table(config.ITEM_DB_PATH, new=True) # If set to True, Flask-SQLAlchemy will track modifications of objects and emit signals. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///var/items.db" # Configure resources/routes api.add_resource(Item, "/item/<string:name>") api.add_resource(ItemList, "/items") api.add_resource(UserRegister, "/register") # Init application
from datetime import timedelta from flask import Flask from flask_bcrypt import Bcrypt from flask_jwt import JWT from sqlalchemy.orm.exc import NoResultFound from lab6.auth import authenticate, identity from lab6.blueprint import api_blueprint from lab6.error_handlers import not_found, server_error, not_authorized app = Flask(__name__) app.config['SECRET_KEY'] = 'super-secret' app.config['JWT_AUTH_URL_RULE'] = '/api/v1/auth' app.config['JWT_EXPIRATION_DELTA'] = timedelta(days=1) app.config['JWT_AUTH_URL_OPTIONS'] = {'methods': ['POST'], 'endpoint': 'auth'} app.register_blueprint(api_blueprint, url_prefix="/api/v1") app.register_error_handler(NoResultFound, not_found) app.register_error_handler(Exception, server_error) jwt = JWT(app, authenticate, identity) jwt.jwt_error_handler(not_authorized) bcrypt = Bcrypt(app)