aiohttp-security/demo/main.py

57 lines
1.7 KiB
Python
Raw Normal View History

2015-11-26 18:09:00 +00:00
import asyncio
from aiohttp import web
from aiohttp_session import setup as setup_session
from aiohttp_session.redis_storage import RedisStorage
from aiohttp_security import setup as setup_security
from aiohttp_security import SessionIdentityPolicy
from aiopg.sa import create_engine
from aioredis import create_pool
from demo.db_auth import DBAuthorizationPolicy
2015-11-30 12:58:49 +00:00
from demo.handlers import Web
2015-11-26 18:09:00 +00:00
@asyncio.coroutine
def init(loop):
redis_pool = yield from create_pool(('localhost', 6379))
dbengine = yield from create_engine(user='aiohttp_security',
password='aiohttp_security',
database='aiohttp_security',
host='127.0.0.1')
app = web.Application(loop=loop)
setup_session(app, RedisStorage(redis_pool))
setup_security(app,
SessionIdentityPolicy(),
DBAuthorizationPolicy(dbengine))
2015-11-30 12:58:49 +00:00
web_handlers = Web()
yield from web_handlers.configure(app)
2015-11-26 18:09:00 +00:00
handler = app.make_handler()
srv = yield from loop.create_server(handler, '127.0.0.1', 8080)
print("Server started at http://127.0.0.1:8080")
2015-11-26 18:26:57 +00:00
return srv, app, handler
2015-11-26 18:09:00 +00:00
@asyncio.coroutine
2015-11-26 18:26:57 +00:00
def finalize(srv, app, handler):
sock = srv.sockets[0]
app.loop.remove_reader(sock.fileno())
sock.close()
yield from handler.finish_connections(1.0)
srv.close()
yield from srv.wait_closed()
yield from app.finish()
2015-11-26 18:09:00 +00:00
2016-02-01 17:25:14 +00:00
def main():
loop = asyncio.get_event_loop()
srv, app, handler = loop.run_until_complete(init(loop))
try:
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete((finalize(srv, app, handler)))