Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing all paths to one handler function at Falcon Framework

I want to routes all paths that start with something like “/api” to same handler function.

Such as:

/api/foo
/api/bar
/api/foo/bar
/api/bar/baz/this/that 

All should be handled with one function and I should be able to get the full path after /api.

This feature is very handy and I used it often at Node.js Express framework. Now I am looking for ways to accomplish the same thing with Python Falcon framework.

More info can be found here; It defines the feature as "white-listed “global” functionality."

http://expressjs.com/api.html#app.all

like image 790
rsa Avatar asked Sep 16 '25 14:09

rsa


1 Answers

Perhaps you are looking for Falcon's sink facility, e.g.:

class Sink(object):
    def on_get(self, req, resp):
        resp.body = ('\nTwo things awe me most, the starry sky '
                     'above me and the moral law within me.\n'
                     '\n'
                     '    ~ Immanuel Kant\n\n')

app = falcon.API()
handler = Sink().on_get
app.add_sink(handler, prefix='/')

This will route all URLs to the sink handler.

like image 158
jorjun Avatar answered Sep 19 '25 03:09

jorjun