unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Learning Guile web. Stuck on returning an image.
@ 2014-05-25 22:30 Martyn Smith
  2014-05-26  0:22 ` Nala Ginrut
  2014-05-26  9:05 ` Neil Jerram
  0 siblings, 2 replies; 4+ messages in thread
From: Martyn Smith @ 2014-05-25 22:30 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1180 bytes --]

Hi guys,

I am attempting to create a website as part of a learning experience with
guile. The code is originally based from ideas in
https://www.gnu.org/software/guile/manual/html_node/Web-Examples.html. My
code has progressed since the "Web Examples" page to support a more MVC
style. So far, so good-ish. :-)

Getting to the point of the email -- I am a bit confused how I would return
a jpg image. For example: -
<img src="/load-image?id=1234" />

The "1234" tells me which file to load and return.

Being primarily a .NET developer, I am struggling to understand how to
achieve such goal in guile. I have tried things like (read-file
"/location/to/image.jpg" "r") but no luck. Also tried to understand
converting to bytes in the documentation but again... no luck. Always
getting a 500 error. Yes, I have included (content-type . (image/jpg)) etc.

Can anyone give me a heads up on how to do such thing? How would I open the
jpg file? How would it be returned?

I don't think its worth providing any code here. I think the Web-Examples
page is the best place to review code, and how I would return an image from
that current codebase, etc.

Thanks for any suggestions.

mart

[-- Attachment #2: Type: text/html, Size: 1585 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: Learning Guile web. Stuck on returning an image.
@ 2015-12-11 11:46 Martyn Smith
  0 siblings, 0 replies; 4+ messages in thread
From: Martyn Smith @ 2015-12-11 11:46 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 2656 bytes --]

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 <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)
> >                      (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!
>
>

[-- Attachment #2: Type: text/html, Size: 3648 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-12-11 11:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-25 22:30 Learning Guile web. Stuck on returning an image Martyn Smith
2014-05-26  0:22 ` Nala Ginrut
2014-05-26  9:05 ` Neil Jerram
  -- strict thread matches above, loose matches on Subject: below --
2015-12-11 11:46 Martyn Smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).