From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Martyn Smith Newsgroups: gmane.lisp.guile.user Subject: Re: Learning Guile web. Stuck on returning an image. Date: Fri, 11 Dec 2015 11:46:13 +0000 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a1130c3fad444b305269dded2 X-Trace: ger.gmane.org 1449834394 25304 80.91.229.3 (11 Dec 2015 11:46:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 11 Dec 2015 11:46:34 +0000 (UTC) Cc: guile-user@gnu.org To: Nala Ginrut Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Dec 11 12:46:33 2015 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1a7M9e-0003SN-57 for guile-user@m.gmane.org; Fri, 11 Dec 2015 12:46:30 +0100 Original-Received: from localhost ([::1]:47078 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a7M9d-0007lU-J6 for guile-user@m.gmane.org; Fri, 11 Dec 2015 06:46:29 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49681) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a7M9T-0007lI-Ty for guile-user@gnu.org; Fri, 11 Dec 2015 06:46:21 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a7M9O-0004ry-OT for guile-user@gnu.org; Fri, 11 Dec 2015 06:46:19 -0500 Original-Received: from mail-ig0-x22c.google.com ([2607:f8b0:4001:c05::22c]:32773) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a7M9O-0004rS-HV for guile-user@gnu.org; Fri, 11 Dec 2015 06:46:14 -0500 Original-Received: by mail-ig0-x22c.google.com with SMTP id mv3so38038956igc.0 for ; Fri, 11 Dec 2015 03:46:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:cc:content-type; bh=zq6tbgY6mi0G9bF01uMX+vfxDhs86FLiBq1scxRhZ7s=; b=w7DdhZFkZ8Lu4Wk2zk+Cq2aGTpQcW5p6owCNb9oVlL75ak6XDJK9+KXf3dcGnJlcFF o3ZCk8A+/GS8ev2ViSHcTitWo7DxfGQXKRR89EJcRJFKeu9pTFOWNFC9ej93GZK7Blh+ NwTry/v3lIV+YWCakpEwRLL+NA3aVwPxH0E+7gvtsL2ngnDNVoSqJW0GSJv+DDEpiu3X Yore4KTPUXvzw+NiZMH6MP17J6arrCSEkuYA/nRuGu86C+8wOnTajBn57P3uALCcD0PN ZsbMTgy+d0Y1UiDG1I1OHYSgIkESnrLi9BKoqkXhQkzXzm0CQESWxe4VrtmpWAhEakdi OgfQ== X-Received: by 10.50.142.99 with SMTP id rv3mr3902012igb.72.1449834373725; Fri, 11 Dec 2015 03:46:13 -0800 (PST) Original-Received: by 10.107.15.162 with HTTP; Fri, 11 Dec 2015 03:46:13 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4001:c05::22c X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12240 Archived-At: --001a1130c3fad444b305269dded2 Content-Type: text/plain; charset=UTF-8 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 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! > > --001a1130c3fad444b305269dded2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Thanks for extra s= uggestions!

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

For my family phot= os, I wanted them to be kept hidden from the public, which is why I mention= ed 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.<= br>
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 f= unction myself in the documentation. Although it has been a while since i l= ast properly reviewed the docs. Probably did not think it would have been u= seful at the time? Regardless, I should check to see what extra cool featur= es are included in Artanis now!

Thanks again,
M



On Fri, Dec 11, 2015 at 6:31 AM, Nala Gin= rut <nalaginrut@gmail.com> 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)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (rnrs io ports))
>
> (init-server)
>
> (get "/image"
>=C2=A0 =C2=A0 =C2=A0 (lambda (rc)
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 (let* ((port (open-file "s.jpg" &= quot;r"))
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (bytes (get-byt= evector-all port)))
>=C2=A0 =C2=A0 =C2=A0 (close-port port)
>=C2=A0 =C2=A0 =C2=A0 (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/ma= nual/manual.html#sec-6-4

Sometimes, you don't want to reveal the jpg filename, say,
(get "/image"
=C2=A0 (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 360= 0)
=C2=A0 (lambda (rc)
=C2=A0 =C2=A0 (:cache rc)))


Happy hacking!


--001a1130c3fad444b305269dded2--