|
From: | Martyn Smith |
Subject: | Re: Learning Guile web. Stuck on returning an image. |
Date: | Fri, 11 Dec 2015 11:46:13 +0000 |
Hi Martyn!
I'm very glad to see a new user of Artanis!
On Thu, 2015-12-10 at 19:44 +0000, Martyn Smith wrote:
> (use-modules (artanis artanis)
> (rnrs io ports))
>
> (init-server)
>
> (get "/image"
> (lambda (rc)
> (let* ((port (open-file "s.jpg" "r"))
> (bytes (get-bytevector-all port)))
> (close-port port)
> (response-emit bytes #:headers '((content-type image/jpg))))))
>
> (run #:port 1234)
>
In this case, you have three more alternatives to try. It is not
recommended to handle static files manually. Such work is treated as
low-level in Artanis.
1. Static files handling
This would be done by (init-server #:statics '(jpg)) or add more static
files ext-filename as you wish.
In this way, you don't have to write rules & handlers, say, you don't
need (get ...). Artanis will handle jpg files and detect correct MIME
for you, and proper cache expires according to your config file.
2. Use emit-response-with-file
http://www.gnu.org/software/artanis/manual/manual.html#sec-6-4
Sometimes, you don't want to reveal the jpg filename, say,
(get "/image"
(lambda (rc) (emit-response-with-file "s.jpg")))
Of course, you don't have to specify MIME, but no cache option (ETag)
specified too. Unless you specify it explicitly. This method is used for
MVC to emit HTML template of Views automatically. Because sometimes we
don't want Artanis to add caching for us cleverly.
3. Take advantage of #:cache shortcut
For static files, we often want to emit it with caching.
(get "/image" #:cache '(public "s.jpg" #:maxage 3600)
(lambda (rc)
(:cache rc)))
Happy hacking!
[Prev in Thread] | Current Thread | [Next in Thread] |