Created
October 21, 2011 16:25
-
-
Save gcr/1304257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
;; Write a program that causes the url said (e.g. | |
;; http://localhost:port/said) to produce a page with an input field | |
;; and a submit button. When the submit button is pressed, that should | |
;; produce a second page with a single link saying “click here.” When | |
;; that is clicked it should lead to a third page that says “you said: | |
;; …” where … is whatever the user typed in the original input field. | |
(require web-server/servlet | |
web-server/servlet-env) | |
(define (start req) | |
;; First, we get input from the user | |
;; Then, we pause for the link | |
;; Finally, we show what the user typed. | |
(let ([input (get-input-from-user)]) | |
(pause-for-link) | |
(show-input input))) | |
(define (render pg) | |
;; Draw a simple HTML page | |
(response/xexpr | |
`(html (head (title "Arc challenge")) | |
(body ,pg)))) | |
(define (get-input-from-user) | |
(extract-binding/single | |
'your-input | |
(request-bindings | |
(send/suspend ;; stops the program here; the program will be resumed when the client visits k-url | |
(λ (k-url) | |
(render `(form ([action ,k-url]) | |
(input ([name "your-input"] [type "text"])) | |
(input ([type "submit"]))))))))) | |
(define (pause-for-link) | |
(send/suspend ;; again, resume the program when the client visits k-url | |
(λ (k-url) | |
(render `(a ([href ,k-url]) | |
"Again, once more"))))) | |
(define (show-input the-text-you-said) | |
(render `(div "You said: " ,the-text-you-said))) | |
;; Launch | |
(serve/servlet start | |
#:servlet-path "/said" | |
#:port 8080) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment