Tornado endpoint matching
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.
@xyres if I call api/v1/tree it will also match the 2nd one, so what I wonder if what is the priority for the matching to happen
– chuseuiti
Jun 29 at 16:59
api/v1/tree
will not match the first handler because it doesn't have a slash at the end. But if you call api/v1/tree/
, it will call the first handler because Tornado won't try to match other endpoints after a match is found.– xyres
Jun 29 at 17:05
api/v1/tree
api/v1/tree/
1 Answer
1
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.
/api/v1/tree/
/api/v1/garden/tree
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Whichever match is found first, tornado will call that handler.
– xyres
Jun 28 at 17:36