Created
May 15, 2014 17:35
-
-
Save aodag/4c87e74505b870d67a3a to your computer and use it in GitHub Desktop.
aiohttpとaiopgを使ってasync web+dbアプリケーション
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import io | |
import argparse | |
import asyncio | |
import aiopg | |
import sqlalchemy as sa | |
import webob | |
from aiopg.sa import create_engine | |
from aiohttp.wsgi import WSGIServerHttpProtocol | |
from webdispatch import URLDispatcher, MethodDispatcher | |
from jinja2 import Environment, PackageLoader | |
templates = Environment(loader=PackageLoader(__name__, 'templates')) | |
engine = None | |
class Application(URLDispatcher): | |
def __init__(self): | |
super(Application, self).__init__() | |
self.add_url('top', '/', index) | |
metadata = sa.MetaData() | |
document_table = sa.Table("documents", metadata, | |
sa.Column("id", sa.Integer, primary_key=True), | |
sa.Column("name", sa.Unicode(255)), | |
sa.Column("contents", sa.UnicodeText), | |
) | |
def async_request(environ): | |
data = yield from environ['wsgi.input'].read() | |
environ['wsgi.input'] = io.BytesIO(data) | |
request = webob.Request(environ) | |
return request | |
def render_response(name, values): | |
tmpl = templates.get_template(name) | |
response = webob.Response(tmpl.render(values)) | |
return response | |
@asyncio.coroutine | |
def get_index(environ, start_response): | |
with (yield from engine) as conn: | |
documents = yield from conn.execute(document_table.select()) | |
response = render_response('index.html', {'documents': documents}) | |
return response(environ, start_response) | |
@asyncio.coroutine | |
def post_index(environ, start_response): | |
request = yield from async_request(environ) | |
with (yield from engine) as conn: | |
tx = yield from conn.begin() | |
yield from conn.execute(document_table.insert().values( | |
name=request.params['name'], | |
contents=request.params['contents'])) | |
documents = yield from conn.execute(document_table.select()) | |
response = render_response('index.html', {'documents': documents}) | |
yield from tx.commit() | |
return response(environ, start_response) | |
index = MethodDispatcher() | |
index.register_app('get', get_index) | |
index.register_app('post', post_index) | |
class AppProtocol(WSGIServerHttpProtocol): | |
def __init__(self): | |
super(AppProtocol, self).__init__(Application()) | |
def main(): | |
global engine | |
parser = argparse.ArgumentParser() | |
parser.add_argument('dsn') | |
parser.add_argument('host') | |
parser.add_argument('port', type=int) | |
args = parser.parse_args() | |
loop = asyncio.get_event_loop() | |
engine = loop.run_until_complete(create_engine(dsn=args.dsn)) | |
print(engine) | |
proto = loop.create_server(AppProtocol, args.host, args.port) | |
server = loop.run_until_complete(proto) | |
print(server) | |
loop.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jinja2==2.7.2 | |
MarkupSafe==0.23 | |
SQLAlchemy==0.9.4 | |
WebDispatch==1.2 | |
WebOb==1.4 | |
aiohttp==0.7.2 | |
aiopg==0.2.1 | |
psycopg2==2.5.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment