get_param = { 'aio': lambda req, name, kwargs: req.match_info[name], 'jap': lambda req, name, kwargs: req.match_dict[name], 'san': lambda req, name, kwargs: kwargs[name], }[engine] get_json_body = { 'aio': lambda req: req.json(), 'jap': lambda req: req.json, 'san': lambda req: req.json, }[engine] make_app = { 'aio': lambda: web.Application(), 'jap': lambda: japronto.Application(), 'san': lambda: sanic.Sanic(), }[engine] add_route = { 'aio': lambda app, method, url, handler: app.router.add_route( method, url, handler), 'jap': lambda app, method, url, handler: app.router.add_route( url, handler, methods=[method]), 'san': lambda app, method, url, handler: app.router.add( url.replace('{', '<').replace('}', '>'), [method], handler) }[engine]
headers=get_headers(), ) def plaintext_view(request): return request.Response( body=b'Hello, world!', mime_type='text/plain', headers=get_headers(), ) async def db_view(request): async with app.db_pool.acquire() as conn: world = await conn.fetchrow("select id,randomnumber from world where id=%s" % random.randint(1, 10000)) return request.Response( text=json.dumps(dict(world)), mime_type='application/json', headers=get_headers()) app = japronto.Application() app.on_startup.append(init_db) app.on_cleanup.append(close_db) app.router.add_route('/json', json_view, 'GET') app.router.add_route('/plaintext', plaintext_view, 'GET') app.router.add_route('/db', db_view, 'GET') if __name__ == '__main__': app.run('0.0.0.0', 8080, worker_num=multiprocessing.cpu_count())