aboutsummaryrefslogtreecommitdiff
path: root/prototypes/c3b2/web.scm
diff options
context:
space:
mode:
Diffstat (limited to 'prototypes/c3b2/web.scm')
-rwxr-xr-xprototypes/c3b2/web.scm50
1 files changed, 29 insertions, 21 deletions
diff --git a/prototypes/c3b2/web.scm b/prototypes/c3b2/web.scm
index 0f72aa0..1c430c3 100755
--- a/prototypes/c3b2/web.scm
+++ b/prototypes/c3b2/web.scm
@@ -23,6 +23,8 @@ exec guile -L $(pwd) -e '(@ (web) main)' -s "$0" "$@"
23(use-modules ((ice-9 match))) 23(use-modules ((ice-9 match)))
24(use-modules ((web request))) 24(use-modules ((web request)))
25(use-modules ((web uri))) 25(use-modules ((web uri)))
26(use-modules ((rnrs bytevectors)))
27(use-modules (ice-9 binary-ports))
26 28
27;; third party 29;; third party
28(use-modules ((fibers web server))) 30(use-modules ((fibers web server)))
@@ -37,34 +39,28 @@ exec guile -L $(pwd) -e '(@ (web) main)' -s "$0" "$@"
37(use-modules ((web html))) 39(use-modules ((web html)))
38(use-modules ((web mime-types))) 40(use-modules ((web mime-types)))
39(use-modules ((web static))) 41(use-modules ((web static)))
42(use-modules ((sha-2)))
40 43
44;; gnunet
45(use-modules ((gnunet sync)))
41 46
42(setlocale LC_ALL "")
43
44;;; helpers
45;;
46;; until we have real messages backend by wt and gnunet
47;;
48 47
49(define %loremipsum (string-split "Suspendisse potenti. Quisque ac orci sed metus molestie ornare. Nam in neque magna. Proin vel consectetur nisl, a suscipit est. Ut eget lectus maximus, scelerisque felis non, gravida leo. Pellentesque nisi risus, posuere vitae elit eget, efficitur euismod risus. Cras pulvinar, nisl vitae tincidunt hendrerit, dui nulla pellentesque libero, vel maximus erat arcu sed dui. Mauris porta dui nec arcu auctor, quis tristique massa dictum. Etiam consequat leo quis tortor vulputate, vitae eleifend lorem tempus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque a enim scelerisque, tristique turpis sed, accumsan velit. Proin eleifend, augue sit amet efficitur volutpat, magna ipsum consequat elit, vel tempor est nulla et sem. Cras faucibus odio ac est vestibulum varius. Sed lobortis fringilla egestas. Vestibulum pulvinar est vel mauris malesuada, at dapibus nibh rutrum. Etiam pellentesque lacus nec sapien pharetra, quis ultrices lorem condimentum!" #\space)) 48(setlocale LC_ALL "")
50 49
51(define (choice lst)
52 (list-ref lst (random (length lst))))
53 50
54(define (loremipsum length) 51(define %configuration #f)
55 (string-join (map (lambda _ (choice %loremipsum)) (iota length)) " ")) 52(define %home #f)
56 53
57(define (make-message body parent) 54;;; helpers
58 `((parent . ,parent)
59 (body . ,body)))
60 55
61(define (make-random-topic) 56(define (make-hash bv)
62 (make-message (loremipsum 99) #f)) 57 (let ((hash (make-sha-512)))
58 (sha-512-update! hash bv)
59 (sha-512-finish! hash)
60 (sha-512->string hash)))
63 61
64;;; 62;;;
65 63
66(define %topics (map (lambda _ (make-random-topic)) (iota 0)))
67
68(define (c3b2/topics) 64(define (c3b2/topics)
69 "Return vertices of topics ie. messages without parent" 65 "Return vertices of topics ie. messages without parent"
70 (map get (fs:find 'parent #f))) 66 (map get (fs:find 'parent #f)))
@@ -73,7 +69,16 @@ exec guile -L $(pwd) -e '(@ (web) main)' -s "$0" "$@"
73 (let ((new-topic `((kind . message) 69 (let ((new-topic `((kind . message)
74 (body . ,body) 70 (body . ,body)
75 (parent . #f)))) 71 (parent . #f))))
76 (save (create-vertex new-topic)))) 72 (let ((topic (save (create-vertex new-topic))))
73 (let* ((payload (vertex-ref topic 'body))
74 (payload (string->utf8 payload))
75 (hash (make-hash payload))
76 (filepath (string-append %home "/" hash)))
77 (call-with-output-file filepath
78 (lambda (port)
79 (put-bytevector port payload))
80 #:binary #t)
81 (publish %configuration filepath '("c3b2://v1/topic"))))))
77 82
78;;; web 83;;; web
79 84
@@ -108,7 +113,7 @@ exec guile -L $(pwd) -e '(@ (web) main)' -s "$0" "$@"
108 (render-html (template "index" (template/index (c3b2/topics))))) 113 (render-html (template "index" (template/index (c3b2/topics)))))
109 114
110(define (route/index/post body) 115(define (route/index/post body)
111 (c3b2/topic-add! (assoc-ref (decode body) "body")) 116 (c3b2/topic-add! (car (assoc-ref (decode body) "body")))
112 (redirect "/")) 117 (redirect "/"))
113 118
114(define (router request body) 119(define (router request body)
@@ -120,7 +125,10 @@ exec guile -L $(pwd) -e '(@ (web) main)' -s "$0" "$@"
120 (_ (not-found (uri->string (request-uri request))))))) 125 (_ (not-found (uri->string (request-uri request)))))))
121 126
122 127
123(define-public (main _) 128(define-public (main args)
129 (set! %configuration (cadr args))
130 (set! %home (caddr args))
131 (pk %configuration %home)
124 (format #t "Server running @ http://localhost:8080\n") 132 (format #t "Server running @ http://localhost:8080\n")
125 (with-env (env-open* "wt" (list *feature-space*) "create,log=(enabled=true)") 133 (with-env (env-open* "wt" (list *feature-space*) "create,log=(enabled=true)")
126 (run-server router))) 134 (run-server router)))