Project

General

Profile

Request Router » History » Version 3

Jan Klopper, 2012-04-10 15:17

1 1 Jan Klopper
h1. The Request Router
2 1 Jan Klopper
3 1 Jan Klopper
µ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.
4 1 Jan Klopper
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.
5 1 Jan Klopper
6 1 Jan Klopper
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.
7 1 Jan Klopper
8 1 Jan Klopper
h2. Some examples
9 2 Jan Klopper
10 1 Jan Klopper
<pre><code class="python">
11 1 Jan Klopper
#!/usr/bin/python
12 1 Jan Klopper
"""Some website"""
13 1 Jan Klopper
14 1 Jan Klopper
# Custom modules
15 1 Jan Klopper
import uweb
16 1 Jan Klopper
import pages
17 1 Jan Klopper
18 1 Jan Klopper
__author__ = 'yourname <yourname@yourdomain.nl>'
19 1 Jan Klopper
__version__ = '0.1'
20 1 Jan Klopper
21 1 Jan Klopper
PACKAGE = 'some_website'
22 1 Jan Klopper
PAGE_CLASS = pages.PageMaker
23 1 Jan Klopper
CONFIG = '../website.conf'
24 1 Jan Klopper
</code></pre>
25 1 Jan Klopper
26 1 Jan Klopper
In the above example we setup a router by doing the following:
27 1 Jan Klopper
* We import uweb,
28 1 Jan Klopper
* We import our module where our Pagemaker class lives.
29 1 Jan Klopper
* We set some copyright / author information (µWeb does not use these, buts its good python practise)
30 1 Jan Klopper
* We set the PACKAGE variable so that the logging module knows what name this website has.
31 1 Jan Klopper
* We set the PAGE_CLASS variable to the class that holds all of the request pages.
32 1 Jan Klopper
* 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.
33 1 Jan Klopper
34 1 Jan Klopper
After this, we will need to setup some actual routers to route incoming requests to the correct pages.
35 1 Jan Klopper
36 1 Jan Klopper
<pre><code class="python">
37 1 Jan Klopper
ROUTES = (
38 1 Jan Klopper
    ('/', 'RequestIndex'),
39 1 Jan Klopper
    # static
40 1 Jan Klopper
    ('(/styles/.*)', 'Static'),
41 1 Jan Klopper
    ('(/js/.*)', 'Static'),
42 1 Jan Klopper
    ('(/media/.*)', 'Static'),
43 1 Jan Klopper
    # Error handling
44 1 Jan Klopper
    ('(/.*)', 'RequestInvalidcommand')
45 1 Jan Klopper
)
46 1 Jan Klopper
</code></pre>
47 1 Jan Klopper
48 1 Jan Klopper
The above routes do the following:
49 1 Jan Klopper
* We send traffic that requests '/' to the method RequestIndex
50 1 Jan Klopper
* 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.
51 1 Jan Klopper
* All other requests will be handled by RequestInvalidcommand, which in our case usually returns a nice 404 page.
52 1 Jan Klopper
53 1 Jan Klopper
As you can see, every match we make in the regex will be a new argument on the recipient method.
54 1 Jan Klopper
55 1 Jan Klopper
To make the actual server run, we do one last command:
56 1 Jan Klopper
<pre><code class="python">
57 1 Jan Klopper
uweb.ServerSetup()
58 1 Jan Klopper
</code></pre>
59 1 Jan Klopper
60 1 Jan Klopper
h2. More examples
61 1 Jan Klopper
62 1 Jan Klopper
How to use various other url formats:
63 1 Jan Klopper
64 1 Jan Klopper
* If you want to match random word like characters, dashes and spaces, and possibly include a trailing / you can use something like this:
65 1 Jan Klopper
** ([\w\- ]+)/? 
66 1 Jan Klopper
* 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.
67 3 Jan Klopper
** /somecommand/([\w\- ]+)/([\w\-]+)/othercommand/?
68 1 Jan Klopper
* If you want to include an optinal page number after a command:
69 1 Jan Klopper
** /somecommand/?(\d+)?
70 1 Jan Klopper
** 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.
71 1 Jan Klopper
72 1 Jan Klopper
h2. Static files
73 2 Jan Klopper
74 1 Jan Klopper
µWeb can handle Static files from disk by itself, you just need to point the routes to the Static method, and give it the correct path where it can find the resource.
75 1 Jan Klopper
The mime-type will be discovered automatically by using the Magic libraries avialable on your server.
76 1 Jan Klopper
77 1 Jan Klopper
h2. OpenID routers
78 2 Jan Klopper
79 1 Jan Klopper
How to setup routes if you want to use the openID module:
80 1 Jan Klopper
* ('/OpenIDLogin/?(\w+)?', '_OpenIdInitiate')
81 1 Jan Klopper
* ('/OpenIDValidate', '_OpenIdValidate')