Skip to content
Snippets Groups Projects
__init__.py 1.42 KiB
Newer Older
  • Learn to ignore specific revisions
  • from flask import Flask, redirect, request
    
    from flask_uploads import configure_uploads
    
    import app.database.models as models
    
    from app.core import bcrypt, db, jwt, ma
    
    from app.core.dto import MediaDTO
    
    Victor Löfgren's avatar
    Victor Löfgren committed
    
    
    def create_app(config_name="configmodule.DevelopmentConfig"):
    
        app = Flask(__name__, static_url_path="/static", static_folder="static")
    
    Victor Löfgren's avatar
    Victor Löfgren committed
        app.config.from_object(config_name)
    
        app.url_map.strict_slashes = False
    
    Victor Löfgren's avatar
    Victor Löfgren committed
        with app.app_context():
    
            bcrypt.init_app(app)
            jwt.init_app(app)
            db.init_app(app)
    
    Victor Löfgren's avatar
    Victor Löfgren committed
            db.create_all()
    
            ma.init_app(app)
    
            configure_uploads(app, (MediaDTO.image_set,))
    
    Victor Löfgren's avatar
    Victor Löfgren committed
            from app.core.sockets import sio
    
            sio.init_app(app)
    
    
            from app.apis import flask_api
    
            flask_api.init_app(app)
    
            @app.before_request
            def clear_trailing():
                rp = request.path
                if rp != "/" and rp.endswith("/"):
                    return redirect(rp[:-1])
    
    Carl Schönfelder's avatar
    Carl Schönfelder committed
            @app.after_request
            def set_core(response):
                header = response.headers
                header["Access-Control-Allow-Origin"] = "*"
                return response
    
    
    Victor Löfgren's avatar
    Victor Löfgren committed
        return app, sio
    
    Victor Löfgren's avatar
    Victor Löfgren committed
    
    
    def identity(payload):
        user_id = payload["identity"]
        return models.User.query.filter_by(id=user_id)
    
    
    @jwt.token_in_blacklist_loader
    def check_if_token_in_blacklist(decrypted_token):
        jti = decrypted_token["jti"]
    
    
        return models.Blacklist.query.filter_by(jti=jti).first() is not None