Skip to content

Instantly share code, notes, and snippets.

@ramitmittal ramitmittal/app.py
Last active Jun 29, 2019

Embed
What would you like to do?
Tornado article series 1-2
import os
import tornado
import motor
from heroes.handlers import *
class Application(tornado.web.Application):
def __init__(self):
handlers = [
("/", HomeHandler),
]
settings = dict(
debug=False,
default_handler_class=ErrorHandler,
)
super(Application, self).__init__(handlers, **settings)
MONGO_CONNECT_URI = os.environ.get('MONGO_CONNECT_URI')
client = motor.motor_tornado.MotorClient(MONGO_CONNECT_URI)
self.db = client.heroes
def main():
tornado.log.enable_pretty_logging()
PORT = os.environ.get('PORT')
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(PORT)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()
import tornado.web
class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
return self.application.db
class HomeHandler(BaseHandler):
def get(self):
self.write({
"message": "The API server is operational."
})
class ErrorHandler(BaseHandler):
def prepare(self):
self.set_status(404)
self.write({
"message": "Could not find the resource requested."
})
self.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.