Skip to content

Instantly share code, notes, and snippets.

@prologic
Last active August 29, 2015 14:22
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 prologic/4d4d23f0ca2f91531db8 to your computer and use it in GitHub Desktop.
Save prologic/4d4d23f0ca2f91531db8 to your computer and use it in GitHub Desktop.
A simple RESTful-ish Comments API based on the ReactJS Tutorial(s)
from os import environ
from json import dumps, loads
from circuits.web import Server, Controller, Static
class Comments(Controller):
channel = "/comments.json"
def index(self, **data):
with open("comments.json", "r") as f:
comments = loads(f.read())
if self.request.method == "POST":
comments.append({"author": data["author"], "text": data["text"]})
with open("comments.json", "w") as f:
f.write(dumps(comments))
self.response.headers["Content-Type"] = "application/json"
self.response.headers["Cache-Control"] = "no-cache"
return dumps(comments)
(Server(("0.0.0.0", int(environ.get("PORT", 3000)))) + Comments() + Static(docroot="public")).run()
@prologic
Copy link
Author

prologic commented Jun 2, 2015

A simple RESTful-ish Comments API based on the ReactJS Tutorial(s)

See: https://github.com/reactjs/react-tutorial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment