unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Ricardo Wurmus <rekado@elephly.net>
To: Gentoo Arch <gentoocore@firemail.cc>
Cc: guile-user@gnu.org
Subject: Re: Operate upon POST request with Guile webserver
Date: Sun, 21 Aug 2022 21:47:16 +0200	[thread overview]
Message-ID: <87r1191ia1.fsf@elephly.net> (raw)
In-Reply-To: <828eadbc-c2bf-c4ab-0833-96c55011d36c@firemail.cc>

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!

-- 
Ricardo



  parent reply	other threads:[~2022-08-21 19:47 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 [this message]
2022-08-21 20:03   ` Gentoo Arch

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=87r1191ia1.fsf@elephly.net \
    --to=rekado@elephly.net \
    --cc=gentoocore@firemail.cc \
    --cc=guile-user@gnu.org \
    /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).