twister is an experiment in using the tornado package in order to create a way to create applications within tornado's web framework and use them together with reverse proxying to essentially create a very loosely coupled web application framework.
the default build process uses the nginx web server.
requirements
Using both virtualenv and virtualenvwrapper is recommended.$ easy_install -a --prefix ./ setuptools PyCrypto>=1.9 \
http://www.lag.net/paramiko/download/paramiko-1.7.4.tar.gz \
http://git.fabfile.org/cgit.cgi/fabric/snapshot/fabric-0.9b1.tar.gz \
pycurl==7.16.2.1 simplejson \
http://www.tornadoweb.org/static/tornado-0.2.tar.gz
Where ./
is the base directory of your virtualenv. Or just leave all
easy_install
flags out if you're not in a virtualenv and want to install globally.to run main app (both are equivalent)
In thetwister
python module directory (next to this README):$ fab start
$ python twister.py
this will run the main app on port 8888. Adding the --daemon
flag will daemonize it (run it in the background.) however, there is a
better way to handle this, described in the "daemon handling" section.debug mode
by default, tornado will scan all *.py files to see if a change has been made. if changes are made, it will restart the server automatically.daemon handling
using the--daemon
flag will put the server in the background and put its PID in tmp/tornado.pid
using fabric, there is a wrapper that handles PID tracking and the start, stop, and restart cmds:
$ fab start
$ fab stop
$ fab restart
alternatively, you could write an init.d
script for each app you need.run more apps
$ python apps/admin/admin.py
this will run the admin app on port 8889.Soon there will be a fabric task that will start all apps as daemons and they will be able to be stopped, started and restarted via fabric.
putting it all together
it is possible to run multiple apps on one server with Apache, nginx or any other capable web server by just writing multiple tornado apps and mounting them at certain locations. ex:/ would point to "frontend" app
/blog/ would point to "blog" app
/admin/ would point to "admin" app
Et cetera. An example Apache VirtualHost:<VirtualHost *>
ServerName whatever.com
ProxyPass /admin/ http://127.0.0.1:8889/
ProxyPass / http://127.0.0.1:8888/
</VirtualHost>
Sample for nginx:server {
server_name whatever.com;
location /admin/ {
proxy_pass http://localhost:8889/;
}
location / {
proxy_pass http://localhost:8888/;
}
}
Where the /
app is always last.nginx
nginx (pronounced as "engine X") is a lightweight, high performance web server/reverse proxy and e-mail (IMAP/POP3) proxy, licensed under a BSD-like license. It runs on UNIX, GNU/Linux, BSD variants, Mac OS X, Solaris, and Microsoft Windows.~ http://en.wikipedia.org/wiki/Nginx
NOTE: pcre is a requirement.
Run this in the current directory (the one this README resides in). This will build and install nginx development version into
build/local
.$ make install
Then to start nginx (both are equivalent):$ ./build/local/sbin/nginx
$ make start
Where the /
app is always last. Note the slash at the end of /admin/
-- this makes it so URLs are easy to consistently parse and match against our regex handlers.static assets
In debug mode, tornado serves thestatic
folder for each app at that location, so: `twister/static/` would be accessible at:
`http://localhost:8888/static/`
or `http://whatever.com/static/` (in our sample VirtualHost)
`twister/apps/admin/static/` would be accessible at:
`http://localhost:8889/static/`
or `http://whatever.com/admin/static/` (in our sample VirtualHost)
Of course when debug=False
for production purposes you'd probably want to add some aliases to the static
folders manually in your VirtualHost, or create a deploy script that does that all for you.url slashes
a common desirable feature is to standardize all dynamic URLs with a forward slash (/
) at the end. twister is pre-configured for this using the tornado.web.addslash
decorator, and only requires that for each separate app that you mount
at a certain URL, you set up an HTTP redirect to forward to the root of
the app. an example is in order:handlers = [
(r"/?", PageHandler),
(r"/(\d+)/?(.*)", PageHandler),
(r"/admin", tornado.web.RedirectHandler, dict(url="/admin/")),
]
the line with /admin
is what you're looking at. since in our VirtualHost above our admin app only grabs urls that start with /admin/
, in this case we need to forward /admin
to /admin/
so our admin app can grab the request and serve it.from https://github.com/patcoll/twister