Skip to content

Instantly share code, notes, and snippets.

@RichardBray
Created October 5, 2018 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBray/91f5651a47a381735a9ee53a7635e046 to your computer and use it in GitHub Desktop.
Save RichardBray/91f5651a47a381735a9ee53a7635e046 to your computer and use it in GitHub Desktop.
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
import json
items = []
class TodoItems(RequestHandler):
def get(self):
self.write({'items': items})
class TodoItem(RequestHandler):
def post(self, _):
items.append(json.loads(self.request.body))
self.write({'message': 'new item added'})
def delete(self, id):
global items
new_items = [item for item in items if item['id'] is not int(id)]
items = new_items
self.write({'message': 'Item with id %s was deleted' % id})
def make_app():
urls = [
("/", TodoItems),
(r"/api/item/([^/]+)?", TodoItem)
]
return Application(urls, debug=True)
if __name__ == '__main__':
app = make_app()
app.listen(3000)
IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment