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

Last change on this file since 15226 was 15226, checked in by certainty, 10 years ago

Changes applied for certainty (88.70.194.195) through svnwiki:

  • Property svnwiki:tags set to web
  • Property svnwiki:title set to uri-dispatch
File size: 1.6 KB
Line 
1=== Description
2
3uri-dispatch is a simple mechanism to dispatch to 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
17Will come soon
18
19=== Examples
20<pre>
21
22(use spiffy intarweb uri-common uri-dispatch)
23
24(define (my-dispatch-error . path)
25  (send-status 404 (sprintf "My Dispatch-error ~A" path)))
26
27
28(module example
29  (echo webiota)
30  (import scheme chicken srfi-1 extras)
31  (require-library spiffy)
32  (import (only spiffy send-status))
33
34  (define (webiota #!optional (start "0") (steps "10") #!rest rest)
35    (send-status 200 (sprintf "webiota called: ~A" (iota (string->number steps) (string->number start)))))
36
37  (define (echo . args)
38    (send-status 200 (sprintf "echo: ~A" args))))
39
40(define (outsidemodule . args) (send-status 200 "Outside module"))
41
42(vhost-map `(("localhost" . ,(lambda (continue)
43                              (parameterize ((handle-not-found  (lambda (path) (dispatch-uri (request-uri (current-request)))))
44                                             (dispatch-error (lambda path (send-status 404 (sprintf "Path not found: ~A" path)))))
45                                (continue))))))
46(start-server)
47
48</pre>
49
50Now start the server and visit the following pages:
51* http://localhost:8080/example/webiota/2/10/
52* http://localhost:8080/example/webiota/2/20/
53* http://localhost:8080/example/webiota/echo/this/please
54* http://localhost:8080/outsidemodule
55* http://localhost:8080/i/do/not/exist
Note: See TracBrowser for help on using the repository browser.