MHT Setup in Tornado Applications
I've been working in Tornado a lot lately. If you don't know of it, it's a Python-based web server/framework written for Friend Feed which has since been open-sourced by Facebook.
The docs do a decent job getting you started, however, to really dig into the application you need to pour over the source code. As Mr. van Rossum says in PEP 8, code is read more often than it is written, and the FriendFeed/Facebook team has done a beautiful job with this application.
So there's a ton of ways to skin a cat in Tornado, but this is the way I like to setup my projects:
. ├── requirements.txt ├── schema.sql ├── src │ ├── app.py │ ├── handlers │ ├── lib │ ├── local_settings.example.py │ ├── local_settings.py │ ├── media │ ├── models │ ├── settings.py │ ├── templates │ └── urls.py └── todo.txt
For lack of a better term, I'm calling this MHT (Model, Handler, Template). Handlers are my controllers, models are, well…models, and templates are the templates!
In the handlers directory, the init.py sets up the BaseHandler, which is a subclass of tornado.web.RequestHandler. In this class, I override get_error_html() to do pretty error pages, and get_current_user() to check for auth. Then all of the other handlers are subclasses of BaseHandler. The app.py file is basically the applications bootstrap file.
So that's the basic idea of how I go about setting up my Tornado applications. It's starting to look more and more like a CodeIgniter or Django project, but for me, that's okay. If you're a Tornado developer, how do you tend to set things up?