What is the correct way to separate routes in Falcon?

80 views
Skip to first unread message

Dmitriy Krasota

unread,
Dec 12, 2016, 8:16:36 AM12/12/16
to Falcon Framework
Examples, provided in documentation describe applications for one or few routes. Each url path has own class. What is the best practice for situation with lots of routes.
For example, I have following RESTFul structure:

localhost:8000/my_part/A
localhost:8000/my_part/B
localhost:8000/my_part/A/default
localhost:8000/my_part/B/special

and so on.

What should I do:
1. For each path create it's own class
2. Create single class for 'my_part' and somehow distinguish A, B in that class (for example with stripping req.uri)
Notice that 'default ' and 'special' are not variables, but actual routes.


What do you suggest?

John Vrbanac

unread,
Jan 7, 2017, 11:36:42 PM1/7/17
to falconf...@googlegroups.com

I think that the number of resource classes and routes you need really depend on how much logic separation you need. Theoretically, you could logically group and collapse them down to a two resources for these two routes:


/my_part/{item}

/my_part/{item}/{item_type}


I know you mentioned that "default" and "special" aren't variables, but it might be an acceptable option to use the route url template to your advantage here and just check the url field passed to the route. At the end of the day, I don't think there really is a "best practice" here. I think it really depends on what you feel is the cleanest and clearest way to keep things organized.


Personally, I tend to use quite a few small resource classes. Take the following example:


/books/{isbn}

/books/{isbn}/characters

/books/{isbn}/characters/{name}

/books/{isbn}/authors

/books/{isbn}/authors/{name}


I would create a small resource class for each one of those routes. Any overlapping functionality would get tossed into a base class or other modules.


Hopefully this helps a little bit.

Reply all
Reply to author
Forward
0 new messages