[racket] Web-Server: Continue Tutorial, Templates and Dispatch

From: Helmut Dobretzberger (h.dobretzberger at gmx.at)
Date: Mon Oct 22 10:32:22 EDT 2012

Hey,

I want to build my new web-application with the racket-webserver, so I have
worked through the continue-tutorial.
Now i want to improve the simple blog from the tutorial to learn more
web-server tecniques which leads to some questions:

- I've tried to change that application to use templates. (It' would be nice
to include that in the tutorial!)
The relevant part of template with all posts look like this:

@in[p posts]{
<div class="post">
      <a href="@????">@(post-title p)
      <p>@(post-body p)</p>
      <div>@(number->string (length (post-comments p))) comment(s)</div>
      </a>
    </div>
 }
</div>

What ist he best way to fill the @???? - because that is a specific url for
every post generated by embed/url in the racket-application so it can't be
accessed with selectors like the other parts.


- As an alternative, I was thinking about using a url like this
"?id=@(post-id p)" but then i read in the docs that it is not recommended to
use extract-binding/single. But how can i create "permanent-urls" for
linking from other sites etc when i should not use get-parameters?

- As a consequence, I've tried the web-server/dispatch with the
small-example in the docs. (It would also be nice if this topic is treated
in the continue-tutorial)
The example runs successful when starting from drracket:

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

(define-values (blog-dispatch blog-url)
  (dispatch-rules
   [("") list-posts]
   [("posts" (string-arg)) review-post]))

(define (list-posts req) 
  (response/xexpr
   `(body "list-posts")))

(define (review-post req p) 
  (response/xexpr
   `(body ,(string-append "review-posts " p))))

(serve/servlet blog-dispatch
               #:servlet-path "/"
               #:servlet-regexp #rx"" ;But is this "the right way"?
               #:launch-browser? #t)    


But - how do I run that on the plt-web-server? I have removed the
serve/servlet and added


(define interface-version 'v1)
(define timeout +inf.0)
(define start blog-dispatch)
(provide interface-version timeout start) 


but that leads to the follwing error:


servlet-error-responder: contract violation
  expected: exn?, given: #<exn:dispatcher>
  in: the 2nd argument of
      (-> url? exn? response?)
  contract from: 
      <collects>/web-server/configuration/responders.rkt
  blaming: 
      <collects>/web-server/web-config-unit.rkt
  at: <collects>/web-server/configuration/responders.rkt:126.2


What do I have to do to run this code on the server?

Thanks,
Helmut





Posted on the users mailing list.