Example #1
0
async def test_allowed_hosts_ok(test_client):
    async def handler(request):
        return web.Response()

    app = web.Application()
    app.router.add_get('/', handler)
    await _setup(app, AllowedHosts({'example.com'}))
    cl = await test_client(app)
    resp = await cl.get('/', headers={'Host': 'example.com'})
    assert resp.status == 200
Example #2
0
async def init(loop):
    conf = load_config(PROJ_ROOT / 'config' / 'config.yml')

    app = web.Application(loop=loop)

    app.router.add_route('GET', "/api/v2/{item}", method)
    app.router.add_route('GET', "/api/{item}", method)
    cache = Cache(plugins=[HitMissRatioPlugin(), TimingPlugin()])

    print(conf['allowed_items'])

    if 'host' in conf:
        host = conf['host']
    else:
        host = '127.0.0.1'

    if 'port' in conf:
        port = conf['port']
    else:
        port = '443'

    if 'access_log_format' in conf:
        access_log_format = conf['access_log_format']
    else:
        access_log_format = '%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'

    if 'scheme' in conf:
        if conf['scheme'] == 'https':
            if 'sslcertchain' and 'sslkey' in conf:
                ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
                ssl_context.load_verify_locations(conf['sslcertchain'],
                                                  conf['sslkey'])
                ssl_context.load_cert_chain(conf['sslcertchain'],
                                            conf['sslkey'])
            else:
                raise NameError(
                    'sslcertchain / sslkey missing in the configuration')
        else:
            ssl_context = None
    else:
        ssl_context = None

    app['config'] = conf

    user, password, realm = conf['authentication']['user'], conf[
        'authentication']['password'], conf['authentication']['realm']
    await setup(app, AllowedHosts(conf['allowed_hosts']),
                BasicAuth(user, password, realm))
    app['cache'] = cache
    return app, cache, host, port, access_log_format, ssl_context
Example #3
0
async def cache():
    conf = load_config(PROJ_ROOT / 'config' / 'config-gunicorn.yml')

    logging.basicConfig(level=logging.DEBUG)
    app = web.Application()

    app.router.add_route('GET', "/api/v2/{item}", method)
    app.router.add_route('GET', "/api/v2/{item}/{domain}", method)
    app.router.add_route('GET', "/api/{item}", method)
    app.router.add_route('GET', "/api/{item}/{domain}", method)
    memcached_host = conf['cache']['memcached_host']
    memcached_port = conf['cache']['memcached_port']
    #cache = Cache(plugins=[HitMissRatioPlugin(), TimingPlugin()])
    lookup_type = {}
    cache = Cache(Cache.MEMCACHED,
                  endpoint=memcached_host,
                  port=memcached_port,
                  serializer=JsonSerializer(),
                  plugins=[HitMissRatioPlugin(),
                           TimingPlugin()])

    if 'statsd' in conf:
        if conf['statsd']['enable']:
            hostname = socket.gethostname().split('.', 1)[0]
            c = statsd.StatsClient(conf['statsd']['host'],
                                   conf['statsd']['port'],
                                   prefix=conf['statsd']['prefix'])
            t = MetricsTimer(conf['statsd']['interval'], cache_metrics, cache,
                             lookup_type, c, hostname)

    app['config'] = conf

    user, password, realm = conf['authentication']['user'], conf[
        'authentication']['password'], conf['authentication']['realm']
    await setup(app, AllowedHosts(conf['allowed_hosts']),
                BasicAuth(user, password, realm))
    app['cache'] = cache
    app['lookup_type'] = lookup_type
    return app