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