Here is the Middleware class:
class Middleware:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
request = Request(environ)
cookies = request.cookies
path = request.path
environ['isAuthenticated'] = isAuthenticated(cookies)
return self.app(environ, start_response)
def isAuthenticated(cookies):
if (len(cookies)) > 0 :
if 'jwt' in cookies:
payload = decodeJWT(cookies['jwt'])
connection = connect_db()
return payload
else:
return False
And here is the ConnectDB.py
:
def connect_db():
host = current_app.config['HOST']
db = current_app.config['DB']
username = current_app.config['USERNAME']
password = current_app.config['PASSWORD']
connection = psycopg2.connect(host=host, database=db, user=username, password=password, port=5432, cursor_factory=RealDictCursor)
return connection
I'm provoking the middleware
likes this:
app = Flask(__name__)
app.config.from_object('SETTINGS')
app.wsgi_app = Middleware(app.wsgi_app)
And I'm getting this error:
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
I know that I need to call connect_db
from within the app context to access the DB credentials but how can I access it from inside the Middleware
class?