* 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.
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
1 sibling, 0 replies; 4+ messages in thread
From: Nala Ginrut @ 2014-05-26 0:22 UTC (permalink / raw)
To: Martyn Smith; +Cc: Guile User
[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]
Hi Martyn!
2014年5月26日 上午6:30于 "Martyn Smith" <martyn.developer@googlemail.com>写道:
>
> 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. :-)
>
If you want to play web with Guile, I suggest Artanis, which is a web
framework for it. It's easier than using the lower web api.
www.web-artanis.com
> 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.
>
Please attache you complete code. But I guess what you need is
get-bytevector-all which dwells on (rnrs).
Usually, open file only return a file port, you have to read the content
then.
> 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: 2190 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Learning Guile web. Stuck on returning an image.
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
1 sibling, 0 replies; 4+ messages in thread
From: Neil Jerram @ 2014-05-26 9:05 UTC (permalink / raw)
To: Martyn Smith; +Cc: guile-user
Martyn Smith <martyn.developer@googlemail.com> writes:
> 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 recently had the same problem, and eventually decided that it would be
better to use Guile only for my dynamic content, with a standard web
server for the static content. For the latter I chose lighttpd, and I
can provide more details of my setup if that would be helpful.
But what specifically was the problem? My handler for an image file
(and other static content) looked like this:
(lambda (request request-body uri)
(let* ((fn (apply string-append
static-root
(map (lambda (name)
(string-append "/" name))
uri)))
(content (with-input-from-file fn read-string))
(content-type (cond ((string-match "\\.svg" fn)
'(image/svg+xml))
((string-match "\\.html?" fn)
'(text/html))
((string-match "\\.jpe?g" fn)
'(image/jpeg))
((string-match "\\.png" fn)
'(image/png))
((string-match "\\.css" fn)
'(text/css))
((string-match "\\.js" fn)
'(application/javascript))
(else
'(text/plain)))))
(trc 'content-type content-type 'length (string-length content))
(values `((content-type . ,content-type))
content)))
The problem here that 'content' contains binary data that can't be
returned as is in an HTTP response. It needs to be encoded in one of
the HTTP-supported encodings, and a corresponding Content-Encoding
header added to the response.
I think I investigated a little whether Guile's (web ...) modules could
do this, or could easily be enhanced, but decided instead on the
solution described above.
Regards,
Neil
^ 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).