-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.janet
More file actions
50 lines (49 loc) · 1.28 KB
/
form.janet
File metadata and controls
50 lines (49 loc) · 1.28 KB
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
(import spork/http)
(defn main
[_ &opt host port]
(default host "127.0.0.1")
(default port 8000)
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/submit
(def form-template
``
<form action="/" method="post">
<pre>
<textarea name="text" rows="5" cols="72">%s</textarea>
<input type="submit" value="send"/>
</pre>
</form>
``)
#
(defn body
[value]
(def result
(cond
(empty? value)
""
#
(string/has-prefix? `You said, "` value)
(string/slice value
(length `You said, "`)
(- (length value) 1))
#
(string/format `You said, "%s"` value)))
#
(string/format form-template result))
#
(defn handler
[request]
(def key-vals
(->> (get request :buffer)
(peg/match http/query-string-grammar)
first))
(def received
(get key-vals "text" "Nothing to see here..."))
#
{:headers {"Content-type" "text/html"}
:status 200
:body (body received)})
#
(printf "Trying to start server at %s:%d" host port)
(http/server handler host port))