Decorator based router for Tornado Web Framework that help reduce development time.
Tornado Web Framework requires handler class to be implemented for every request as shown in below example.
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
This is okay for small applications but when you are writing big applications with hundreds of handlers, things quickly get out of hand. Especially since we have to pay extra attention to ensure indentation is correct.
tornado_router addresses this issue by providing API to implement decorator-based router. Now, you can type less and pay little attention to indentation. Same example can be reduced to below example.
@router.route()
def index(handler):
handler.write("Hello, world")
It has no performance disadvantages compared to class handlers since it constructs handler classes on server startup.
Please click on Star if you like this library =)
tornado_router also provides other optional extra features to cut development time.
- Asynchronous authentication and authentication redirect
@router.route(auth=True)
- JSON request
@router.route(json=True)
- Custom URL Routing (or else URL defaults to method name)
@router.route(url="index")
- Different HTTP Methods supported (GET, HEAD, POST, DELETE, PATCH, PUT, OPTIONS)
@router.route(method="post")
- Disable XSRF validation
@router.route(xsrf=False)
Below code implements basic authentication
import tornado_router
import tornado.web
import tornado.httpserver
router = tornado_router.Router(base_handler=tornado_router.BaseHandler)
@router.route(method='get', url='/')
def index(handler):
""" index page """
handler.write('hello tornado!')
@router.route(method='get', url='/auth_required', auth=True)
def auth_required(handler):
""" if not authenticated, redirected to login page """
handler.write('authenticated!')
@router.route()
def login(handler):
""" login page """
handler.write('login!')
def main():
# print all requests
print(router.requests)
app = tornado.web.Application(router.handlers)
app.listen(9000)
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
main()
- Python 3.4+
- tornado
You are more than welcome to make any contributions. Please create Pull Request for any changes.
The tornado_router is offered under MIT license.