Search code examples
tornado

Tornado endpoint matching


I have been trying to find some documentation from tornado about endpoint matching priorities and I couldnt find anything..I wonder what is the expected behaviour of tornado doing endpoint matching.

Example:

def make_app():
    return tornado.web.Application(
            (r"/api/v1/tree/", test1),
            (
                r"/api/v1/?(?P<variable1>[A-Za-z0-9-]+)?/?(?P<variable2>[A-Za-z0-9-]+)?",
                test2,
            ),
            (r"/api/v1/garden/tree/" + r"([^/]+)/", test3)
            ]
     )

In particular I wonder if the 1st and 3rd method will be ever called or if the second call will make the others to be ignored.


Solution

  • All rules are considered in order and the first match is used. So in this case the /api/v1/tree/ rule will always be considered. The /api/v1/garden/tree rule should probably be moved above the second rule, although it's hard to read the regular expression to determine whether there is a real conflict there.