gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet-scheme] 21/49: examples/web: Respond with HTML.


From: gnunet
Subject: [gnunet-scheme] 21/49: examples/web: Respond with HTML.
Date: Sat, 25 Dec 2021 22:59:58 +0100

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit f5d5053579ba06aaadaf5a6c238d1d883f09211a
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Sat Sep 25 15:22:32 2021 +0200

    examples/web: Respond with HTML.
    
    It makes the interface a little nicer, and it will help with
    creating forms for submitting GETs and PUTs.
    
    * examples/web.scm
      (respond/html, estimate->html): New procedures.
      (url-handler): Use it instead of outputting plain text.
---
 examples/web.scm | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/examples/web.scm b/examples/web.scm
index a70f3f4..b39cdba 100644
--- a/examples/web.scm
+++ b/examples/web.scm
@@ -17,21 +17,39 @@
              #:prefix #{nse:}#)
             ((gnu gnunet dht client)
              #:prefix #{dht:}#)
+            (web response)
             (web server)
-            (srfi srfi-11))
+            (srfi srfi-11)
+            (sxml simple))
 
 (define config (load-configuration))
 
+(define (respond/html body)
+  (values (build-response
+          #:code 200
+          #:headers `((content-type application/xhtml+xml) (charset . 
"utf-8")))
+         (lambda (port)
+           (display "<!DOCTYPE html>\n" port)
+           (sxml->xml `(html (@ (xmlns "http://www.w3.org/1999/xhtml";))
+                             (head (title "Hello"))
+                             (body ,body))
+                      port))))
+
+(define (estimate->html estimate)
+  `(dl (dt "Timestamp")
+       (dd ,(number->string (nse:estimate:timestamp estimate)))
+       (dt "Number of peers")
+       (dd ,(number->string (nse:estimate:number-peers estimate)))
+       (dt "Standard deviation")
+       (dd ,(number->string (nse:estimate:standard-deviation estimate)))))
+
 (define (url-handler server request body)
   (define current-estimate (nse:estimate server))
   (define body
     (if current-estimate
-       (format #f "timestamp: ~a~%number peers: ~a~%stddev logarithm: ~a"
-               (nse:estimate:timestamp current-estimate)
-               (nse:estimate:number-peers current-estimate)
-               (nse:estimate:standard-deviation current-estimate))
-       "no estimate available yet ..."))
-  (values '((content-type text/plain)) body #f))
+       (estimate->html current-estimate)
+       '(p "No estimate yet")))
+  (respond/html body))
 
 (define (start config)
   (define nse-server (nse:connect config))

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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