Skip to content

Instantly share code, notes, and snippets.

@malinich
Last active February 15, 2018 19:30
Show Gist options
  • Save malinich/d729b78f79a8a8c706812e10ebd5e25a to your computer and use it in GitHub Desktop.
Save malinich/d729b78f79a8a8c706812e10ebd5e25a to your computer and use it in GitHub Desktop.
tornado umongo
# main.py
class WliBlogApplication(Application):
db = AsyncIOMotorClient().wli
db_instance = MotorAsyncIOInstance()
db_instance.init(db)
def main():
return WliBlogApplication()
if __name__ == '__main__':
app = main()
tornado.platform.asyncio.AsyncIOMainLoop().install()
app.listen(3001)
asyncio.get_event_loop().run_until_complete(create_index())
asyncio.get_event_loop().run_forever()
# handlers
class BlogList(BaseHandler):
async def get(self, *args, **kwargs):
blogs = await Blog.find({})
self.write(blogs)
async def post(self, *args, **kwargs):
data = tornado.escape.json_decode(self.request.body)
blog = Blog(**data)
inserted_id = await blog.commit()
self.write("%s" % inserted_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment