Request Router » History » Version 1
Version 1/6
-
Next » -
Current version
Jan Klopper, 2012-04-10 15:16
The Request Router¶
µWeb is build around a so called MVC design, which often involves a routing layer that takes any inbound requests for pages, and delegates them to the appropriate methods inside the application.
The µWeb router is based on regexes that match urls, and possibly extract various parts of the url to hand over to the pagemaker as arguments.
Next to the routes, the router is also the startpoint for both the mod_python and standalone versions of µWeb, and because of that its the point where we accept various global settings that define where µWeb should load the templates, where the pagemaker can be found, and how the logging should work.
Some examples
#!/usr/bin/python
"""Some website"""
# Custom modules
import uweb
import pages
__author__ = 'yourname <yourname@yourdomain.nl>'
__version__ = '0.1'
PACKAGE = 'some_website'
PAGE_CLASS = pages.PageMaker
CONFIG = '../website.conf'
In the above example we setup a router by doing the following:
#!/usr/bin/python
"""Some website"""
# Custom modules
import uweb
import pages
__author__ = 'yourname <yourname@yourdomain.nl>'
__version__ = '0.1'
PACKAGE = 'some_website'
PAGE_CLASS = pages.PageMaker
CONFIG = '../website.conf'
- We import uweb,
- We import our module where our Pagemaker class lives.
- We set some copyright / author information (µWeb does not use these, buts its good python practise)
- We set the PACKAGE variable so that the logging module knows what name this website has.
- We set the PAGE_CLASS variable to the class that holds all of the request pages.
- We set the config path, so µWeb can find and load the settings for for example mysql, mongo or settings you use in your application.
After this, we will need to setup some actual routers to route incoming requests to the correct pages.
ROUTES = (
('/', 'RequestIndex'),
# static
('(/styles/.*)', 'Static'),
('(/js/.*)', 'Static'),
('(/media/.*)', 'Static'),
# Error handling
('(/.*)', 'RequestInvalidcommand')
)
The above routes do the following:
- We send traffic that requests '/' to the method RequestIndex
- We send anything that begins with either /styles/ /js/ or /media/ to the built in Static handler, from where µWeb will try to read the file including the styles/js or media part.
- All other requests will be handled by RequestInvalidcommand, which in our case usually returns a nice 404 page.
As you can see, every match we make in the regex will be a new argument on the recipient method.
To make the actual server run, we do one last command:
uweb.ServerSetup()
More examples¶
How to use various other url formats:
- If you want to match random word like characters, dashes and spaces, and possibly include a trailing / you can use something like this:
- ([\w\- ]+)/?
- If you want to match multiple words, and send them as arugments the Method like this: /somecommand/userX/projectY/edit/ use the following, again with the optional slash at the end.
- '/somecommand/([\w\- ]+)/([\w\-]+)/othercommand/?'
- If you want to include an optinal page number after a command:
- /somecommand/?(\d+)?
- This will for example match: /somecommand, /somecommand/10 /somecommand/10/ and in both the last two send 10 as the first argument to the method processing the request.