#lang scheme/base (require web-server/servlet-env web-server/dispatchers/dispatch web-server/managers/lru (planet untyped/dispatch:1)) ; Site ------------------------------------------- ; A "site" is a collection of "controller procedures" that act as entry points to your app. ; ; You typically define this in a separate module and require that module into all of your ; controller and view modules. See the docs for more the recommended pattern. ; site (define-site blog-site ([(url "/") index] [(url "/post/" (integer-arg)) view-post]) ; This is one of the defaults you have to override. It tells Dispatch to pass the request ; back to the web server if no match is found for the URL. The web server will look for matching ; filenames in the static content directories passed to serve/servlet. #:rule-not-found (lambda (request) (next-dispatcher))) ; Controllers ------------------------------------ ; Controllers have to be defined with the define-controller macro. ; See the docs for the various syntaxes. ; request -> response (define-controller (index request) '(html (head (title "Index")) (body (p "This is the index.")))) ; request integer -> response (define-controller (view-post request num) `(html (head (title "Post" ,(number->string num))) (body (p "This is post " ,(number->string num) ".")))) ; Start the application -------------------------- ; request -> response ; ; This is the main entry point for the app. ; It tells Dispatch to match on the URL and call the correct controller. (define (start request) (dispatch request blog-site)) ; request -> response ; ; Technically speaking, this ought to send a 404 code to the browser. (define (file-not-found request) '(html (head (title "Not found")) (body (p "The URL you requested was not found in any " "dynamic or static content in this server.")))) ; This starts the web server: (serve/servlet start ; These arguments tell the server to pass all requests to the application. ; This lets yourely on Dispatch to determine whether a URL should be handled dynamically: #:servlet-path "/" #:servlet-regexp #rx"" ; This argument tells the server what to do if the request URL doesn't match ; any dynamic content *or* static content form the filesystem. #:file-not-found-responder file-not-found)