guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Learning Guile web. Stuck on returning an image.


From: Martyn Smith
Subject: Re: Learning Guile web. Stuck on returning an image.
Date: Fri, 11 Dec 2015 11:46:13 +0000


Thanks for extra suggestions!

I am using the (init-server #:static ...) for my website as it includes css, js, images, etc.

For my family photos, I wanted them to be kept hidden from the public, which is why I mentioned the "/image/{id}" at the end. You have to be logged in to the site in order to view them, as well as various other features it supports.

With that in mind -- I will be trying emit-response-with-file when I next have the source code in front of me. This is a better approach rather than handling mime, etc. I am surprised I did not come across this function myself in the documentation. Although it has been a while since i last properly reviewed the docs. Probably did not think it would have been useful at the time? Regardless, I should check to see what extra cool features are included in Artanis now!

Thanks again,
M



On Fri, Dec 11, 2015 at 6:31 AM, Nala Ginrut <address@hidden> wrote:
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!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]