Reputation: 745
I'm trying to learn about servers using Racket, and I'm getting caught up on trying to use static assets. From this answer, I was able to include a static stylesheet like so:
#lang racket
(require web-server/servlet
web-server/servlet-env
web-server/configuration/responders)
(define (home req)
(response/xexpr
'(html
(head (link ([rel "stylesheet"] [type "text/css"] [href "/style.css"])))
(body
(span ([class "emph"]) "Hello, world!")))))
(define-values (dispatch input-url)
(dispatch-rules
[("home") home]
[("style.css") (λ (_) (file-response 200 #"OK" "style.css"))]))
(serve/servlet dispatch
#:servlet-regexp #rx""
#:servlet-path "/home"
#:server-root-path (current-directory))
However, I'm still confused as to how to do this in general, i.e. serving all files in #:extra-files-paths
without making a dispatch rule for each of them. I tried Jay's advice and changed the dispatcher order in the definition of serve/servlet
by moving the htdocs and extra-files-paths parts up (I probably shouldn't copy that whole thing here) and I broke the ability to resolve MIME types somehow. Overall it was a mess.
So any of these questions would be related/relevant to my problem (from less to more general):
Is there a better way to include static files using tools at the level of serve/servlet
?
Can anyone outline specifically how I might rearrange the pieces in serve/servlet
without breaking things?
Is there a better place than the docs to learn about how to use the lower level server tools in Racket? (I'm pretty new in this particular area so "learn more about servers" may be a valid response to this question)
Upvotes: 4
Views: 521
Reputation: 17203
It looks to me like the problem is your #:servlet-regexp, which is set to the empty regexp, meaning that it will match anything. One easy solution is to restrict this regexp so that it only matches the non-static assets; then, all of the other requests should be served from the #:extra-files-paths.
Perhaps there's some reason why you need to intercept all requests and handle them in your code? Not sure.
Upvotes: 1