----------------------------------------------------------------------
#lang web-server
(require web-server/templates
web-server/dispatch
web-server/web-server)
(define-values (blog-dispatch blog-url) ;;blog-dispatch is our
;;new dispatcher?
(dispatch-rules
[("") list-posts]
[("posts" (string-arg)) review-post]
[("archive" (integer-arg) (integer-arg)) review-archive]
[else list-posts]))
(define (list-posts req) `(list-posts))
(define (review-post req p) `(review-post ,p))
(define (review-archive req y m) `(review-archive ,y ,m))
(define (start)
(serve
#:dispatch blog-dispatch ;;Start the server with our dispatcher?
#:port 8080))
(start) ;;Doesn't start a server : (
----------------------------------------------------------------------
I cant seem to get a server running that is using that dispatcher... Also
I presume its possible to mix "pretty" urls made by dispatch-rules and
embed/url ?
Thanks,
Jordan
_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users
----------------------------------------------------------------------
#lang web-server
(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))))
;; ;; Doesn't work
;; (serve/servlet blog-dispatch
;; #:port 8080
;; #:launch-browser? #t)
;; ;; Doesn't work
;; (serve/servlet blog-dispatch
;; #:port 8080
;; #:servlet-path "/"
;; #:launch-browser? #t)
;; Works
(serve/servlet blog-dispatch
#:port 8080
#:servlet-regexp #rx".*" ;But is this "the right way"?
#:launch-browser? #t)
----------------------------------------------------------------------
Once I got it working, it makes sense; but maybe a more complete example
could be added to the docs?
Shalom,
Yes, I think so. I would guess that URL based dispatch is a common entry
point for people getting started with the web server, as it more closely
resembles other frameworks / languages. The need to capture top-level
requests, and what happens when you don't was a surprise to me. Now that
I "get it" the docs seem very clear.
I first tried out URL dispatch with web-server/insta like so:
#lang web-server/insta
(define-values (blog-dispatch blog-url)
(dispatch-rules
[("") list-posts]
...
[else list-posts]))
(define (list-posts req)
(response/xexpr
`(list-posts)))
...
(define (start req)
(blog-dispatch req))
To no avail. Then I thought since it was called dispatch-rules, and
blog-dispatch I must be creating a customer dispatcher. But there wasn't
much on dispatchers in Web Applications, after abit I found the HTTP
Server docs, and read all of them, much of which was close to being over
my head. Then I tried different combinations of things for a few hours.
I think it would have helped me alot to have an example of URL dispatch
working in a server, as well as the url->request examples. The
url->request set of examples are great to see how dispatch-rules is
handling types, and creating a bi-directional mapping, but it doesn't
help much getting it working in a servlet.
I don't know where it would fit, but one of the things I most missed in
the docs was a more "complete" example of servlet usage, something mixing
dispatch-rules and embed/url, running multiple servlets, and making use
of #:servlet-regexp. Also I guess its possible to run stateful and
stateless servlets together? If so that would be a nice addition to a
more "complete" example.
Overall its a really nice frameworks, and great docs... I am just finding
it to be a steep learning curve and I thought I knew alot of languages /
frameworks.
Thanks,
Jordan