From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: romel@lavabit.com Newsgroups: gmane.lisp.guile.user Subject: Re: Serving files with guile web server Date: Sat, 19 Mar 2011 12:47:13 -0400 (EDT) Message-ID: <18101.189.215.48.232.1300553233.squirrel@lavabit.com> References: <9838.187.147.43.113.1300318403.squirrel@lavabit.com> <58164.187.147.43.113.1300373729.squirrel@lavabit.com> <874o6z97gg.fsf@ossau.uklinux.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1300553287 28568 80.91.229.12 (19 Mar 2011 16:48:07 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 19 Mar 2011 16:48:07 +0000 (UTC) Cc: guile-user@gnu.org To: "Neil Jerram" Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Mar 19 17:48:02 2011 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Q0zJq-0000VT-56 for guile-user@m.gmane.org; Sat, 19 Mar 2011 17:48:02 +0100 Original-Received: from localhost ([127.0.0.1]:50866 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q0zJp-00013S-NB for guile-user@m.gmane.org; Sat, 19 Mar 2011 12:48:01 -0400 Original-Received: from [140.186.70.92] (port=51823 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q0zJl-00013M-8U for guile-user@gnu.org; Sat, 19 Mar 2011 12:47:58 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q0zJj-0003pi-Gd for guile-user@gnu.org; Sat, 19 Mar 2011 12:47:57 -0400 Original-Received: from karen.lavabit.com ([72.249.41.33]:40647) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q0zJj-0003ci-9U for guile-user@gnu.org; Sat, 19 Mar 2011 12:47:55 -0400 Original-Received: from a.earth.lavabit.com (a.earth.lavabit.com [192.168.111.10]) by karen.lavabit.com (Postfix) with ESMTP id D353E11B948; Sat, 19 Mar 2011 11:47:18 -0500 (CDT) Original-Received: from lavabit.com (189.215.48.232.cable.dyn.cableonline.com.mx [189.215.48.232]) by lavabit.com with ESMTP id 21301CK7L3OC; Sat, 19 Mar 2011 11:47:13 -0500 Original-Received: from 189.215.48.232 (SquirrelMail authenticated user romel) by lavabit.com with HTTP; Sat, 19 Mar 2011 12:47:13 -0400 (EDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=lavabit; d=lavabit.com; b=UpCPTu2LsXMiE7LGbN2oGFBzIIEBxfE71GpuGLwF0JKOhU+r9WdAs+beZLb5ZbkHukSnjgJGxoZU/ztsvanIGkWlsyKEGN4vin4JMJGXMBDvWy70yOWYsteYbH/OHDPA7rIuALrWXyg+Dl0EyZOXkQ/4oupbmdeS9y8KEQaFP84=; h=Message-ID:In-Reply-To:References:Date:Subject:From:To:Cc:User-Agent:MIME-Version:Content-Type:Content-Transfer-Encoding; In-Reply-To: <874o6z97gg.fsf@ossau.uklinux.net> User-Agent: SquirrelMail/1.4.13 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 72.249.41.33 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:8544 Archived-At: 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 >> 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 usin= g 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