Last active
August 29, 2015 14:02
-
-
Save Deepwalker/f26fe4ea8aa74cfe3806 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import gevent.monkey | |
gevent.monkey.patch_all() | |
from gevent.wsgi import WSGIServer | |
import pp | |
import ujson | |
pool = pp.PostgresConnectionPool("dbname=api-performance-challenge_development", maxsize=20) | |
HEAD = ('id', 'text', 'created_at', 'likes_count', 'liked') | |
def application(environ, start_response): | |
status = '200 OK' | |
likes = set(i[0] for i in pool.fetchall('SELECT post_id FROM likes WHERE user_id = 1')) | |
posts = pool.fetchall('SELECT id, text, created_at, likes_count FROM posts LIMIT 100') | |
body = ujson.dumps({ | |
'posts': [dict(zip(HEAD, p + (p[0] in likes,))) for p in posts] | |
}) | |
headers = [ | |
('Content-Type', 'application/json') | |
] | |
start_response(status, headers) | |
return [body] | |
WSGIServer(('', 8080), application).serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment