Created
August 23, 2018 02:42
-
-
Save cowbert/e5cf9fef52e24d2b2e330112deba6ed1 to your computer and use it in GitHub Desktop.
tornado virtual url path patching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import tornado bits | |
import tornado.ioloop | |
import tornado.web | |
# Import some controllers | |
from controllers import RequestHandler1, RequestHandler2, RequestHandler3 | |
# original routes | |
routes = [ | |
(r'/path1', RequestHandler1), | |
(r'/path2', RequestHandler2), | |
(r'/path3', RequestHandler3) | |
] | |
""" | |
# Apache config | |
<Location /path/to/app> | |
ProxyPass http://localhost:8888/path/to/app | |
ProxyReverse http://localhost:8888/path/to/app | |
</Location> | |
# you can probably have apache proxy from a different url I suppose | |
""" | |
PREFIX = '/path/to/app' | |
# you can stick PREFIX in a config file, just inlined here for demo | |
for i, orig_route in enumerate(routes): | |
""" | |
* orig_route[0] is the original url path | |
* we take that, prepend with PREFIX | |
* rebuild the route tuple using iterable := tuple.__add__(iterable) | |
""" | |
routes[i] = ( (PREFIX + orig_route[0],) + orig_route[1:] ) | |
app = tornado.web.Application(routes) | |
if __name__ == '__main__': | |
app.listen(8888) | |
tornado.ioloop.IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment