aboutsummaryrefslogtreecommitdiff
path: root/examples/nse-web.scm
blob: 8d9d810cf186dab733d1dd78f1468b9dfa0f7dbe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
;; Copyright (C) 2021 Maxime Devos
;; SPDX-License-Identifier: FSFAP
;; Copying and distribution of this file, with or without modification,
;; are permitted in any medium without royalty provided the copyright
;; notice and this notice are preserved.  This file is offered as-is,
;; without any warranty.

(use-modules (fibers)
	     (gnu gnunet config db)
	     (gnu gnunet config fs)
	     (rnrs hashtables)
	     (gnu gnunet nse client)
	     (web server)
	     (srfi srfi-11))

(define config (load-configuration))

(define (url-handler server request body)
  (define current-estimate (estimate server))
  (define body
    (if current-estimate
	(format #f "timestamp: ~a~%number peers: ~a~%stddev logarithm: ~a"
		(estimate:timestamp current-estimate)
		(estimate:number-peers current-estimate)
		(estimate:standard-deviation current-estimate))
	"no estimate available yet ..."))
  (values '((content-type text/plain)) body #f))

(define (start config)
  (define nse-server (connect config))
  (define impl (lookup-server-impl 'fiberized))
  (define server (open-server impl `(#:port 8089)))
  (define (url-handler* request body)
    (url-handler nse-server request body))
  (let loop ()
    (let-values (((client request body)
		  (read-client impl server)))
      (spawn-fiber
       (lambda ()
	 (let-values (((response body state)
		       (handle-request url-handler* request body '())))
	   (write-client impl server client response body)))))
    (loop)))

(run-fibers (lambda () (start config)))