Skip to content

AioHttp Integration

Using the standard client

option 1: using environment variables

from config.ext.aiohttp import AioHttpConfig

from aiohttp import web


app = web.Application()
routes = web.RouteTableDef()


@routes.get('/')
def home(request):
    return web.Response(text="Hello, world")


@routes.get('/consul')
def consul(request):
    return web.json_response(dict(
        consul_port=app['config'].get('spring.cloud.consul.port'),
        consul_host=app['config']['spring']['cloud']['consul']['host']
    ))

@routes.get('/config')
def config(request):
    return web.json_response(
        dict(config=app['config'])
    )

AioHttpConfig(app)
app.add_routes(routes)
web.run_app(app)

option 2: using custom client

from config.spring import ConfigClient
from config.ext.aiohttp import AioHttpConfig

from aiohttp import web

app = web.Application()
routes = web.RouteTableDef()

@routes.get('/')
def home(request):
    return web.Response(text="Hello, world")


@routes.get('/consul')
def consul(request):
    return web.json_response(dict(
        consul_port=app['cloud_config'].get('spring.cloud.consul.port'),
        consul_host=app['cloud_config']['spring']['cloud']['consul']['host']
    ))

@routes.get('/config')
def config(request):
    return web.json_response(
        dict(config=app['cloud_config'])
    )

AioHttpConfig(
    app,
    key='cloud_config',
    client=ConfigClient(app_name='simpleweb000')
)
app.add_routes(routes)
web.run_app(app)

Using the CloudFoundry client

Set APP_NAME environment variable, for example.

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

option 1: using environment variables

from config.cf import CF
from config.ext.aiohttp import AioHttpConfig

from aiohttp import web


app = web.Application()
routes = web.RouteTableDef()


@routes.get('/')
def home(request):
    return web.Response(text="Hello, world")


@routes.get('/consul')
def consul(request):
    return web.json_response(dict(
        consul_port=app['config'].get('spring.cloud.consul.port'),
        consul_host=app['config']['spring']['cloud']['consul']['host']
    ))

@routes.get('/config')
def config(request):
    return web.json_response(
        dict(config=app['config'])
    )

AioHttpConfig(app, client=CF())
app.add_routes(routes)
web.run_app(app)

option 2: using custom client

from config.cf import CF
from config.spring import ConfiClient
from config.ext.aiohttp import AioHttpConfig

from aiohttp import web


app = web.Application()
routes = web.RouteTableDef()


@routes.get('/')
def home(request):
    return web.Response(text="Hello, world")


@routes.get('/consul')
def consul(request):
    return web.json_response(dict(
        consul_port=app['config'].get('spring.cloud.consul.port'),
        consul_host=app['config']['spring']['cloud']['consul']['host']
    ))

@routes.get('/config')
def config(request):
    return web.json_response(
        dict(config=app['config'])
    )

AioHttpConfig(app, client=CF(client=ConfigClient(app_name='simpleweb000')))
app.add_routes(routes)
web.run_app(app)