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

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue Oct 23 13:48:11 EDT 2012

On Mon, Oct 22, 2012 at 8:32 AM, Helmut Dobretzberger
<h.dobretzberger at gmx.at> wrote:
> 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.

Templates have all of Racket and the scope of the including location
available to them. You can just do

@embed/url[view-post-handler]

and it will work

>
> - 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?

I'd recommend using web-server/dispatch for that purpose.

> - 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?

Dynamically required servlets (that use 'plt-web-server') can't
customize the server itself. The use of web-server/dispatch requires
the server to be customized. (In particular, 'plt-web-server' uses a
cgi-bin-like URL structure to associate URLs with servlets and the
whole point of web-server/dispatch is to not do that.)

You probably shouldn't use the 'plt-web-server' cmdline program for
serious sites.

Jay

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

Posted on the users mailing list.