aboutsummaryrefslogtreecommitdiff
path: root/module/server/test.scm
diff options
context:
space:
mode:
Diffstat (limited to 'module/server/test.scm')
-rw-r--r--module/server/test.scm80
1 files changed, 80 insertions, 0 deletions
diff --git a/module/server/test.scm b/module/server/test.scm
new file mode 100644
index 00000000..33951695
--- /dev/null
+++ b/module/server/test.scm
@@ -0,0 +1,80 @@
+(add-to-load-path "..")
+
+(use-modules (util)
+ (web server)
+
+ (web response)
+ (web request)
+ (web uri)
+ (ice-9 control)
+ (ice-9 regex)
+ (server util)
+ (server macro)
+
+ (ice-9 iconv)
+
+ (sxml simple)
+ (ice-9 ftw))
+
+(define *name* "")
+
+(define (form-page)
+ `(div
+ (p "hello" ,*name*)
+ (form (@ (action "/form")
+ (method POST))
+ (input (@ (type text)
+ (name name)))
+ (input (@ (type submit))))))
+
+(define (sxml->xml-string sxml)
+ (with-output-to-string
+ (lambda () (sxml->xml sxml))))
+
+(define routes
+ (make-routes
+
+ (GET "/" (name)
+ (return
+ '((content-type text/plain))
+ (format #f "No root page, ~a~%" name)))
+
+ (GET "/form" ()
+ (return
+ '((content-type text/html))
+ (sxml->xml-string (form-page))))
+
+ (POST "/form" ()
+ (when (memv 'application/x-www-form-urlencoded (assoc-ref r:headers 'content-type))
+ (apply (lambda* (#:key name #:allow-other-keys)
+ (format #t "*name* := [~a] Received [~a]~%" *name* name)
+ (set! *name* name))
+ (parse-query (uri-decode (bytevector->string body "UTF-8")))))
+ (return (build-response
+ #:code 303
+ #:headers `((location . ,(string->uri-reference "/form"))))
+ ""))
+
+
+ (GET "/ls" ()
+ (return
+ '((content-type text/html))
+ (sxml->xml-string
+ `(table
+ (thead
+ (th (td "Name") (td "Type") (td "Perm")))
+ (tbody
+ ,@(map (lambda (kv)
+ (let* (((k stat) kv))
+ `(tr (td ,k)
+ (td ,(stat:type stat))
+ (td ,(number->string (stat:perms stat) 8)))))
+ (cddr (file-system-tree "." (lambda (p _) (string=? p "."))))))))))
+
+
+ (GET "/ls/:file" (file)
+ (return '((content-type text/plain))
+ (call-with-input-file (string-append "./" file)
+ (@ (ice-9 rdelim) read-string))))))
+
+(run-server routes)