From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Gentoo Arch Newsgroups: gmane.lisp.guile.user Subject: Re: Operate upon POST request with Guile webserver Date: Sun, 21 Aug 2022 23:03:46 +0300 Message-ID: <14d8fe47-6c20-9474-91f2-a0f220b02fc5@firemail.cc> References: <828eadbc-c2bf-c4ab-0833-96c55011d36c@firemail.cc> <87r1191ia1.fsf@elephly.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3562"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Cc: guile-user@gnu.org To: Ricardo Wurmus Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Aug 21 22:04:34 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oPrBR-0000iZ-56 for guile-user@m.gmane-mx.org; Sun, 21 Aug 2022 22:04:33 +0200 Original-Received: from localhost ([::1]:54338 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oPrBQ-0002L5-27 for guile-user@m.gmane-mx.org; Sun, 21 Aug 2022 16:04:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58956) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPrB1-0002KT-83 for guile-user@gnu.org; Sun, 21 Aug 2022 16:04:07 -0400 Original-Received: from [37.120.193.124] (port=49698 helo=mail.cock.li) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPrAv-0007Vf-OD for guile-user@gnu.org; Sun, 21 Aug 2022 16:04:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=firemail.cc; s=mail; t=1661112229; bh=3In8QeWJLKaxog716zfkdf7J+b1/b4ELaluczElXoAg=; h=Date:Subject:To:References:Cc:From:In-Reply-To:From; b=BVHqoba+9qh19zxtBrRQ4lw9eH0TMo1BD0NLJmmOI/AZvh1TVOc5H4f6dB98Nymvv dxO82IvbitGLdZzc0S6I28cTylEetMuu9ppIpfzBAW+/H2q+txzjNcRFFkQ22UiMkL Bh9Yg28iRrpEhePoGSHWd9YBGICANFaycHAnJnCPfgSFY62oRkSqp7Y3TmP2139LfR YkmDNIBL1WZQlzDtOwCmaudJ/RwyID6bXYVeqdJO5OKK3nL7uST5mYU+4f6+HDNOay keC7QIhVGfWdenwF22Lh44oq/5AO/rg8SGRzYEQa79DSmjfVxkvSnna5fzm4x0zsWL zKwWvr7B5sQaA== Content-Language: en-US In-Reply-To: <87r1191ia1.fsf@elephly.net> X-Host-Lookup-Failed: Reverse DNS lookup failed for 37.120.193.124 (failed) Received-SPF: pass client-ip=37.120.193.124; envelope-from=gentoocore@firemail.cc; helo=mail.cock.li X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.001, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:18538 Archived-At: Hi Ricardo, Thank you, that really looks like a prototype for MVC app I wanted to craft. On 8/21/22 22:47, Ricardo Wurmus wrote: > Hi, > > Gentoo Arch writes: > >> I'm trying to figure out how Guile webserver works to develop a simple >> BBS  and I'm kinda stuck with POST request. >> >> My Guile script generates a form on any path and the form sends to >> "/post" path, where I can easily render content sent by the form. > Daniel already solved your immediate problem, but I wanted to show you a > common pattern many of us use to build web servers: > > --8<---------------cut here---------------start------------->8--- > (define* (render-html sxml #:optional (headers '())) > (list (append '((content-type . (text/html))) headers) > (lambda (port) > (sxml->xml sxml port)))) > > (define (request-path-components request) > (split-and-decode-uri-path (uri-path (request-uri request)))) > > (define (controller request body) > (match (cons (request-method request) > (request-path-components request)) > (('POST "api" "container" id "launch") > (render-html `(html > (head (title "whatever")) > (body "who cares")))) > (('PUT "api" "container" id "connect") > ...) > (('DELETE "api" "container" id) > ...) > (('GET "api" "containers") > ...) > (('GET "api" "container" id "info") > ...) > (_ (not-found (request-uri request))))) > > (define (handler request body) > (format (current-error-port) > "~a ~a~%" > (request-method request) > (uri-path (request-uri request))) > (if (getenv "DEBUG") > (call-with-error-handling > (lambda () > (apply values (controller request body)))) > (apply values (controller request body)))) > > (define (run-my-server port) > (run-server handler #:port port)) > --8<---------------cut here---------------end--------------->8--- > > The controller uses match on the method and the path, which makes for > pretty clear code. You can bind parts of the path to variables and > operate on them. > > The “values” stuff is handled in “handler” here, but it also be done > directly in the controller. > > To operate on the POST payload, you’d need to parse the form data from > the body, which is available to the controller. See also guile-webutils > for some handy tools. > > Hope this helps! >