Esempio n. 1
0
def init(loop, argv):
    ap = argparse.ArgumentParser()
    commandline.standard_argparse_options(ap,
                                          default_config='./config/polls.yaml')
    #
    # define your command-line arguments here
    #
    options = ap.parse_args(argv)

    config = commandline.config_from_options(options, TRAFARET)

    # setup application and extensions
    app = web.Application(loop=loop)

    # load config from yaml file in current dir
    app['config'] = config

    # setup Jinja2 template renderer
    aiohttp_jinja2.setup(app,
                         loader=jinja2.PackageLoader('aiohttpdemo_polls',
                                                     'templates'))

    # create connection to the database
    app.on_startup.append(init_pg)
    # shutdown db connection on exit
    app.on_cleanup.append(close_pg)
    # setup views and routes
    setup_routes(app)
    setup_middlewares(app)

    # tracing
    trace_app(app, tracer, service='async-web')
    return app
Esempio n. 2
0
async def init_app():
    app = web.Application()

    setup_aiohttp_apispec(app,
                          title='Web Application',
                          url='/spec.json',
                          swagger_path='/swagger')

    app.router.add_get('/', index_handler)
    app.router.add_get('/ping', ping_handler, allow_head=False)
    app.router.add_post('/free_text', free_text_handler)

    trace_app(app, tracer, service='web-service')

    return app
Esempio n. 3
0

async def handle(request):
    response = {}

    for subject in request.query.getall('subject', []):

        try:
            thought = await think(subject)
            response[subject] = {
                'error': False,
                'quote': thought.quote,
                'author': thought.author,
            }
        except KeyError:
            response[subject] = {
                'error':
                True,
                'reason':
                'This subject is too complicated to be resumed in one sentence.'
            }

    return web.json_response(response)


app = web.Application()
app.router.add_get('/', handle)

trace_app(app, tracer, service='thinker-microservice')
app['datadog_trace']['distributed_tracing_enabled'] = True
web.run_app(app, port=8000)
Esempio n. 4
0
    else:
        statsd.increment("guestbook.view")
        entries = app.redis.lrange(redis_list, 0, -1)
        return {"entries": entries}


async def clear_entries(request):
    redis_list = get_list(request)
    statsd.increment("guestbook.clear")
    app.redis.ltrim(redis_list, 1, 0)
    return web.HTTPFound("/")


app = web.Application()
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader("templates"))

app.redis = redis.StrictRedis(host=redishost,
                              port=redisport,
                              db=0,
                              charset="utf-8",
                              decode_responses=True)
app.redis.config_set("save", "1 1")

app.add_routes(
    [web.route("*", "/", main_page),
     web.post("/clear", clear_entries)])

trace_app(app, tracer, service=service_name)
app['datadog_trace']['analytics_enabled'] = analytics
web.run_app(app)