[Top][All Lists]

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

Re: Serving files with guile web server

From: romel
Subject: Re: Serving files with guile web server
Date: Sat, 19 Mar 2011 12:47:13 -0400 (EDT)
User-agent: SquirrelMail/1.4.13

Please see comments below.

Neil Jerram writes:
> address@hidden 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)
                     `((content-type . (,mime-type-symbol)))
                     (lambda (out-port)
                       (call-with-input-file file-path
                         (lambda (in-port)
                           (display (read-delimited "" in-port)
                     `((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!

Romel Sandoval

reply via email to

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