Last active
February 15, 2018 19:30
-
-
Save malinich/d729b78f79a8a8c706812e10ebd5e25a to your computer and use it in GitHub Desktop.
tornado umongo
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
# 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