aboutsummaryrefslogtreecommitdiff
path: root/module/server/test.scm
blob: f28b11521f76c22c2a79af6293c4b65dffffeeb8 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(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)
             (srfi srfi-88)

             (sxml simple)
             (ice-9 ftw))

(define (form-page name)
  `(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 state))))

   (POST "/form" (name)
         (return (build-response
                  #:code 303
                  #:headers `((location . ,(string->uri-reference "/form"))))
                 "" name))


   (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 'http '() "Default Name")