unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: romel@lavabit.com
To: "Neil Jerram" <neil@ossau.uklinux.net>
Cc: guile-user@gnu.org
Subject: Re: Serving files with guile web server
Date: Sat, 19 Mar 2011 12:47:13 -0400 (EDT)	[thread overview]
Message-ID: <18101.189.215.48.232.1300553233.squirrel@lavabit.com> (raw)
In-Reply-To: <874o6z97gg.fsf@ossau.uklinux.net>

Please see comments below.

Neil Jerram writes:
> romel@lavabit.com writes:
>
>> I gave a second read to the manual and found:
>>
>> "The handler should return two values: the response, as a <response>
>> record from (web response), and the response body as a string,
>> bytevector,
>> or #f if not present."

This is true but I found that when it comes about text is better to use a
lambda that receive a port and then write text to that port.

>>
>> If this is correct then I guess I should check the file type first and
>> then read the file to either create a string or a bytevector from it.

Is important to check the file type, if you need to serve an image you
must use the correct mime-type and a bytevertor.

>>
>> Any recommendation will be highly appreciated.
>
> Well, I suppose I'd recommend considering not using the Guile server to
> serve static files.  Using Guile makes more sense when you want to
> generate dynamic content, because then you're really using the
> capabilities of the language.
>
> In my web server experiments so far I've used Apache, modlisp and Guile
> together, and this combination seems to work well.  Then Apache can
> handle all the static content (I guess using the efficient sendfile)
> without bothering modlisp or Guile, but dynamic stuff can be routed via
> modlisp to Guile.
>

I think that's a good idea but while I was trying to setup my Apache I
found it complex compared to setting up something for python or java using
a web framework (maybe because of the lack of tutorials, examples,
frameworks, etc.), so I think that it would be great to have a standalone
server so you can instant hack something, or even for production, I think
it could be scalable using clusters.

So far I have been able to serve text files and images, it should work
with any mime-type now, but I have not test it yet.

Here is the piece of code that handles files for the curious:

	(let ((file-path (public-file-path path)))
	  (if (file-exists? file-path)
	      (let* ((mime-type (mime-type-ref file-path))
		     (mime-type-symbol (mime-type-symbol mime-type)))
		(if (text-mime-type? mime-type)
		    (values
		     `((content-type . (,mime-type-symbol)))
		     (lambda (out-port)
		       (call-with-input-file file-path
			 (lambda (in-port)
			   (display (read-delimited "" in-port)
                                                    out-port)))))
		    (values
		     `((content-type . (,mime-type-symbol)))
		     (call-with-input-file file-path
			 (lambda (in-port)
			   (get-bytevector-all in-port))))))
	      (not-found request)))

I hope it's readable :-)

Guile web module it's great!

Thanks,
Romel Sandoval






  reply	other threads:[~2011-03-19 16:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-16 23:33 Serving files with guile web server romel
2011-03-17 14:55 ` romel
2011-03-19  2:20   ` Neil Jerram
2011-03-19 16:47     ` romel [this message]
2011-03-19 18:17       ` Neil Jerram
2011-03-19 22:23         ` romel
2011-03-31 14:44       ` Andy Wingo
2011-03-22  8:51     ` Thien-Thi Nguyen
2011-03-31 14:23       ` Andy Wingo
2011-03-31 15:09         ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18101.189.215.48.232.1300553233.squirrel@lavabit.com \
    --to=romel@lavabit.com \
    --cc=guile-user@gnu.org \
    --cc=neil@ossau.uklinux.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).