Friday, September 3, 2010

DrRacket: a first attempt at a web server

This is my first attempt at  a web server using DrRacket. It is supposed to calculate VAT given an input of Net or VAT. It doesn't actually do any calculations, but it does respond to buttons and suchlike.

#lang racket

(require web-server/formlets
         web-server/servlet
         web-server/servlet-env)

(define init-input-string (λ(val)
                            (to-string
                             (required
                              (text-input #:value
                                          (string->bytes/locale
                                           val))))))



#|

(define vat-page%
  (class object%
    (super-new)
    (init-field (rate "17.5") (amount "0"))
   
   
    (define vat-formlet
      (formlet
       (div "Amount: " ,{input-string . => . amount}
            (p "")
            "VAT Rate: " ,{(init-input-string "17.5") . => . rate}
            (p "Still in form. Hello Brooks."))
       #t))

   
    (define (button-clicked request)
      ;(define-values (amount rate)
        (formlet-process vat-formlet request)
      (print "insert-post-handle")
      (print amount)
      (render (redirect/get))      )
   
    (define/public (render request)
      `(html (head (title "My Calculator"))
             (body
              (h1 "Simple VAT calculator")
              (form ([action ,(make-url button-clicked)])
                    ,@(formlet-display vat-formlet)
                    (input [(type "submit") (value "Calculate")]))
              (p ,amount)
              (p ,rate))))))

(define (render-vat-page-new request)
  (define vat-page (make-object vat-page%))
  (define (response-generator make-url)
    (send vat-page render make-url))
  (send/suspend/dispatch response-generator))

(serve/servlet render-vat-page-new #:port 80 #:listen-ip #f)

|#



(define vat-formlet
      (formlet
       (div "Amount: " ,{input-string . => . amount}
            (p "")
            "VAT Rate: " ,{(init-input-string "17.5") . => . rate}
            (p "Still in form. Hello Brooks."))
       (values amount rate)))

(define (render-vat-page amount rate request)
  (local [(define (response-generator make-url)
            `(html (head (title "My Calculator"))
                   (body
                    (h1 "Simple VAT calculator")
                    (form ([action
                            ,(make-url button-clicked)])
                          ,@(formlet-display vat-formlet)
                          (input [(type "submit") (value "Calculate")]))
                    (p ,amount)
                    (p ,rate))))
         
          (define (button-clicked request)
            (define-values (amount rate)
              (formlet-process vat-formlet request))
            (print "insert-post-handle")
            (print amount)
            (render-vat-page amount rate (redirect/get)))]
   
    (send/suspend/dispatch response-generator)))


(serve/servlet (λ(request) (render-vat-page "0.0" "17.5" request)) #:port 80 #:listen-ip #f)


No comments: