Flask Integration¶
Note
This guide simply points out the differences between standalone Relé and the Flask integration. The basics about publishing and consuming are described in the First Steps section.
Setup¶
To configure Relé, our settings may look something like:
RELE = {
'GC_CREDENTIALS_PATH': 'photo_project/settings/dummy-credentials.json',
'MIDDLEWARE': [
'rele.contrib.LoggingMiddleware',
'rele.contrib.FlaskMiddleware',
],
'APP_NAME': 'photo-imaging',
}
# Later when we setup rele and flask:
app = Flask()
rele.config.setup(RELE, flask_app=app)
The only major difference here is that we are using the rele.contrib.FlaskMiddleware
and
that we pass the Flask app
instance to rele.config.setup
method.
Subscribing¶
Now that that the middleware is setup our subscriptions will automatically have Flask’s app context pushed when they are invoked so you will have access to the database connection pool and all other app dependent utilities.
from models import File
from database import db
@sub(topic='photo-uploads')
def handle_upload(data, **kwargs):
new_file = File(data)
db.session.add(new_file)
db.session.commit()