The RegexApplication matches URLs against a list of regular expressions and automatically calls callback methods:
from colubrid import RegexApplication, HttpResponse, execute
class MyApplication(RegexApplication):
urls = [
(r'^$', 'index'),
(r'^hello/$', 'hello_name'),
(r'^hello/(.*?)/$', 'hello_name')
]
def index(self):
return HttpResponse('Hello from the index page!')
def hello_name(self, name='World'):
return HttpResponse('Hello %s!' % name)
app = MyApplication
if __name__ == '__main__':
execute(app)
On request, the RegexApplication iterates through the list of regular expressions and matches the request path against them. (Note: The leading slash is dropped)
The application compiles the list of URLs first, so if the application runs inside a persistent server (FastCGI...) it doesn't have to recompile them on each request.
You can also define a module import path as callback. In this case the RegexApplication will import the function first and call it afterwards.
parts/index.py:
from colubrid import HttpResponse
def display(req):
return HttpResponse('welcome from page.index.display')
And this from inside your application main method:
from colubrid import RegexApplication, execute
class MyApplication(RegexApplication):
urls = [
(r'^$', 'parts.index.display')
]
app = MyApplication
if __name__ == '__main__':
execute(app)
As you can see, the RegexApplication provides the request object as first parameter to the callback method using module callbacks.
Like all other applications except BaseApplication, this application type provides a solution for the trailing slash problem.
This is the example from above with the automatic slash append functionality:
from colubrid import RegexApplication, HttpResponse, execute
class MyApplication(RegexApplication):
urls = [
(r'^$', 'index'),
(r'^hello/$', 'hello_name'),
(r'^hello/(.*?)/$', 'hello_name')
]
slash_append = True
def index(self):
return HttpResponse('Hello from the index page!')
def hello_name(self, name='World'):
return HttpResponse('Hello %s!' % name)
app = MyApplication
if __name__ == '__main__':
execute(app)
When you set slash_append to true your application will behave like typical django applications.