| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 | import multiprocessingfrom wsgiref.handlers import format_date_timeimport randomimport japrontoimport ujson as jsonfrom db import init_db, close_dbdef get_headers():    return {        'Server': 'Japronto/0.1.1',        'Date': format_date_time(None),    }def json_view(request):    return request.Response(        text=json.dumps({'message': 'Hello, world!'}),        mime_type='application/json',        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())
 |