Skip to content

Flask Integration

Using the standard client

option 1: using environment variables

First run: export APP_NAME=simpleweb000

from config.ext.flask import FlaskConfig

from flask import Flask, jsonify


app = Flask(__name__)
FlaskConfig(app)


@app.route('/')
def home():
    return "Hello World!"


@app.route('/consul')
def consul():
    return jsonify(
        consul_port=app.config.get('spring.cloud.consul.port'),
        consul_host=app.config['spring']['cloud']['consul']['host']
    )

@app.route('/config')
def config():
    return jsonify(
        config=app.config.get('spring')
    )

option 2: using custom client

from config.spring import ConfigClient
from config.ext.flask import FlaskConfig

from flask import Flask, jsonify

app = Flask(__name__)
FlaskConfig(app, ConfigClient(app_name='simpleweb000'))

@app.route('/')
def home():
    return "Hello World!"


@app.route('/consul')
def consul():
    return jsonify(
        consul_port=app.config.get('spring.cloud.consul.port'),
        consul_host=app.config['spring']['cloud']['consul']['host']
    )

@app.route('/config')
def config():
    return jsonify(
        config=app.config.get('spring')
    )

Using the CloudFoundry client

option 1: using environment variables

Set APP_NAME=app_name environment variable as others necessary.

For details, see: https://config-client.amenezes.net/docs/1.-overview/#setup

from config.cf import CF
from config.ext.flask import FlaskConfig

from flask import Flask, jsonify

app = Flask(__name__)
FlaskConfig(app, CF())

@app.route('/')
def home():
    return "Hello World!"


@app.route('/consul')
def consul():
    return jsonify(
        consul_port=app.config.get('spring.cloud.consul.port'),
        consul_host=app.config['spring']['cloud']['consul']['host']
    )

@app.route('/config')
def config():
    return jsonify(
        config=app.config
    )

option 2: using custom client

from config.cf import CF
from config.spring import ConfiClient
from config.ext.flask import FlaskConfig

from flask import Flask, jsonify

app = Flask(__name__)
FlaskConfig(app, CF(client=ConfigClient(app_name='simpleweb000')))

@app.route('/')
def home():
    return "Hello World!"


@app.route('/consul')
def consul():
    return jsonify(
        consul_port=app.config.get('spring.cloud.consul.port'),
        consul_host=app.config['spring']['cloud']['consul']['host']
    )

@app.route('/config')
def config():
    return jsonify(
        config=app.config
    )