source: project/wiki/eggref/4/uri-dispatch @ 15376

Last change on this file since 15376 was 15376, checked in by certainty, 12 years ago

Changes applied for certainty (82.82.131.238) through svnwiki:

  • Property svnwiki:tags set to web
  • Property svnwiki:title set to uri-dispatch
File size: 2.8 KB
Line 
1=== Description
2
3uri-dispatch is a simple mechanism to dispatch uris to procedures.
4This is useful for webprogramming where you might want to map uris of a certain kind
5to procedures that implement the logic for those uris.
6
7=== Author
8
9[[David Krentzlin]]
10
11=== Requirements
12
13Requires the [[uri-common]] and [[environments]] extensions.
14
15=== Documentation
16
17<procedure>(dispatch-uri URI)</procedure>
18
19The main interface to dispatch-uri. This procedure invokes the dispatch-mechanism with the supplied uri.
20The dispatch-algorithm is implemented as follows:
21
22*  The path is empty. It checks if '''default-dispatch-target''' is non-false.
23   If it is, then it is invoked. If it is not '''dispatch-error''' is invoked.
24
25* The path has exactly one element. It assumes that this is this element is the name
26   of a thunk that was defined outside a module. If the such a thunk exists it is invoked.
27   If no such a thunk exists it invokes '''dispatch-error'''.
28
29* The path contains at least two elements.
30** It checks if the second element is a procedure defined
31  in a module named by the first element. So if it sees (foo bar) it tries to find the procedure foo
32  inside the module bar. If the procedure exists it is invoked with the rest of the elements as arguments.
33**  If it does not exist, it interpretes the first element as the name of a procedure that was defined
34  outside any module. If such a procedure exists, it is invoked passing the rest of the elements as arguments.
35  If no such procedure exists '''dispatch-error''' is invoked with the full path as argument.
36
37
38=== Examples
39<pre>
40
41(use spiffy intarweb uri-common uri-dispatch)
42
43(define (my-dispatch-error . path)
44  (send-status 404 (sprintf "My Dispatch-error ~A" path)))
45
46
47(module example
48  (echo webiota)
49  (import scheme chicken srfi-1 extras)
50  (require-library spiffy)
51  (import (only spiffy send-status))
52
53  (define (webiota #!optional (start "0") (steps "10") #!rest rest)
54    (send-status 200 (sprintf "webiota called: ~A" (iota (string->number steps) (string->number start)))))
55
56  (define (echo . args)
57    (send-status 200 (sprintf "echo: ~A" args))))
58
59(define (outsidemodule . args) (send-status 200 "Outside module"))
60
61(vhost-map `(("localhost" . ,(lambda (continue)
62                              (parameterize ((handle-not-found  (lambda (path) (dispatch-uri (request-uri (current-request)))))
63                                             (dispatch-error (lambda path (send-status 404 (sprintf "Path not found: ~A" path)))))
64                                (continue))))))
65(start-server)
66
67</pre>
68
69Now start the server and visit the following pages:
70* http://localhost:8080/example/webiota/2/10/
71* http://localhost:8080/example/webiota/2/20/
72* http://localhost:8080/example/webiota/echo/this/please
73* http://localhost:8080/outsidemodule
74* http://localhost:8080/i/do/not/exist
Note: See TracBrowser for help on using the repository browser.