[racket] Counter servlet
I thought I'd try to implement a stateful counter web app using DrRacket. Here's
my code:
#lang racket
;;;; define a really simply counter
;;; required libraries
(require web-server/formlets
web-server/servlet
web-server/servlet-env)
(define (counter request)
(let ((count 0))
(define (response-generator embed/url)
(set! count (+ 1 count))
`(html (head (title "Counter"))
(body
(p "Computer says: " ,(number->string count))
,(embed/url (lambda (request)
(counter (redirect/get))))
(p "Enjoy!"))))
(send/suspend/dispatch response-generator)))
(define log-file
(path->string
(build-path (expand-user-path "~")
"racket-server-access.txt")))
; would this work better?:
; e.g. (list (build-path "js") (build-path "css"))
;;; Start the server
(serve/servlet counter
#:port 8080
#:listen-ip #f
#:log-file log-file
#:servlet-path "/counter.rkt")
My first question is (they'll be more in subsequent post): how do I create a
link in the part of the code that reads
,(embed/url (lambda (request)
(counter (redirect/get))))
because so far my browser returns the text
Computer says: 1/counter.rkt;((%22k%22%20.%20%22(1%201%2064623065)%22))
Enjoy!