[racket] webserver mapping servlets to path
To those familiar with Web Server,
I am a bit lost trying to map servlets to paths. e.g.:
servlet1.rkt should requests to http://localhost:8080/app1
servlet2.rkt should requests to http://localhost:8080/app2
etc.
First, I tried the following:
(require "servlet1.rkt" "servlet2.rkt") ; they export servlet1-start
and servlet2-start
(define-values (blog-dispatch blog-url)
(dispatch-rules
[("app1") servlet1-start]
[("app2") servlet2-start]
[else say-hello]))
(define (say-hello req)
(response/xexpr
`(html (head (title "Hello world!"))
(body (p "Hey out there!")))))
(define (start request)
(blog-dispatch request))
(serve/servlet start
#:command-line? #t
#:launch-browser? #t
#:quit? #t
#:listen-ip #f
#:port 8080
#:log-file "log"
#:extra-files-paths (list (build-path
"/Users/../../multi-serve" "htdocs"))
#:servlet-regexp #rx"")
It sort of works but the PROBLEM with the above is, requests for
static files are dispatched to say-hello. QUESTION: Is there a way to
write a handler to serve static files? So I can replace say-hello with
such procedure. (I think putting the Web Server behind Apache might
work but have not tested it yet. Have Apache serve the static files
first...)
Second, I tried the following:
(define (dispatch request)
(define paths (map path/param-path (url-path (request-uri request))))
(define 2nd-path (if (< (length paths) 2)
""
(second paths)))
(cond
[(equal? "app1" 2nd-path) (servlet1-start request)]
[(equal? "app2" 2nd-path) (servlet2-start request)]
[else (say-hello request)]))
(define (say-hello request)
(response/xexpr
`(html (head (title "Hello world!"))
(body (p "How is it going?")))))
(define (start request)
(dispatch request))
(serve/servlet start
#:command-line? #t
#:launch-browser? #t
#:quit? #t
#:listen-ip #f
#:port 8080
#:log-file "log"
#:extra-files-paths (list (build-path
"/Users/../../multi-serve" "htdocs"))
#:servlet-path "/servlets")
It does handle the static files but now the PROBLEM is that the server
thinks http://localhost:8080/servlets/app1 is static file not found on
the system instead of passing it to this servlet.
I would be very grateful if some one knows a solution or a better way
to go about it.
jGc