root/main.scm

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
(setlocale LC_ALL "")
(use-modules (web client)
             (web request)
             (web response)
             (web uri)
             (ice-9 match)

             (plugin captcha-booru)
             (plugin captcha-plaintext)

             (generate-page)
             (render captcha-example)
             (webserver-utils)
             (utils))

(define (make-index-page)
  '((h1 "captchas")
    (ul
     (li (a (@ (href "/touhou")) "touhou"))
     (li (a (@ (href "/touhou-no-sample")) "touhou no sample"))
     (li (a (@ (href "/random-text-copy")) "random text copy"))
     (li (a (@ (href "/wordlist-text-copy")) "wordlist text copy")))))

(define (handler request body)
  (match (request-path-components request)
    ;; routing
    ('()
     (respond-main-page #f #f #:columns (list (make-index-page))))
    ('("touhou")
     (let ((result (make-captcha-page captcha-touhou request body)))
       (respond-main-page #f #f
                          #:message (assoc-ref result 'message)
                          #:columns (assoc-ref result 'columns))))
    ('("touhou-no-sample")
     (let ((result (make-captcha-page captcha-touhou-no-sample request body)))
       (respond-main-page #f #f
                          #:message (assoc-ref result 'message)
                          #:columns (assoc-ref result 'columns))))
    ('("random-text-copy")
     (let ((result (make-captcha-page captcha-plaintext request body)))
       (respond-main-page #f #f
                          #:message (assoc-ref result 'message)
                          #:columns (assoc-ref result 'columns))))
    ('("wordlist-text-copy")
     (let ((result (make-captcha-page captcha-plaintext-with-wordlist request body)))
       (respond-main-page #f #f
                          #:message (assoc-ref result 'message)
                          #:columns (assoc-ref result 'columns))))
    (else
     (not-found request))))

(define (main-fibers args)
  (use-modules (fibers web server))
  (run-server handler #:port 8084))

(define (main args)
  (use-modules (web server))
  (run-server handler 'http '(#:port 8084)))