From: Gentoo Arch <gentoocore@firemail.cc>
To: Ricardo Wurmus <rekado@elephly.net>
Cc: guile-user@gnu.org
Subject: Re: Operate upon POST request with Guile webserver
Date: Sun, 21 Aug 2022 23:03:46 +0300 [thread overview]
Message-ID: <14d8fe47-6c20-9474-91f2-a0f220b02fc5@firemail.cc> (raw)
In-Reply-To: <87r1191ia1.fsf@elephly.net>
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 <gentoocore@firemail.cc> 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!
>
prev parent reply other threads:[~2022-08-21 20:03 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-17 11:50 Operate upon POST request with Guile webserver Gentoo Arch
2022-08-19 11:00 ` Daniel Meißner
2022-08-20 12:03 ` Gentoo Arch
2022-08-21 19:47 ` Ricardo Wurmus
2022-08-21 20:03 ` Gentoo Arch [this message]
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=14d8fe47-6c20-9474-91f2-a0f220b02fc5@firemail.cc \
--to=gentoocore@firemail.cc \
--cc=guile-user@gnu.org \
--cc=rekado@elephly.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).