[racket] webserver mapping servlets to path
Thank you all for your help.
Dropping else clause did the trick for the first one. This is great
since it's much more elegant than my second way. (BTW, for that second
way, adding #:servlet-regexp #rx"servlets*" did "fix" my issue the
server not finding /servlets/blog as a static file.)
Going back to the tutorials.
jGc
On Mon, Apr 18, 2011 at 9:11 AM, Jay McCarthy <jay.mccarthy at gmail.com> wrote:
> Hi jGc,
>
> It is actually simpler than Noel suggests. Scroll down...
>
> 2011/4/16 J G Cho <gcho at fundingmatters.com>:
>> 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]))
>
> This line says that for _all other requests_ say-hello will respond.
>
>>
>> (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"")
>
> This last line says that the servlet will handle all requests that match #rx"".
>
> These two together mean that the servlet handles all requests and
> say-hello handles everything that doesn't match app1 or app2. Thus,
> "static file requests" never make it to the file dispatcher that is
> created with serve/servlet.
>
> I suggest that you change start so it is explicitly rejects handling
> those requests with (next-dispatcher). One way to do that is to remove
> the 'else' case from the dispatch-rules.
>
> Jay
>
>>
>>
>> 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
>> _________________________________________________
>> For list-related administrative tasks:
>> http://lists.racket-lang.org/listinfo/users
>>
>
>
>
> --
> 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
>