summaryrefslogtreecommitdiff
path: root/hw6/app.moon
blob: 98b15b7faf66c4b1c46558ad689c5f9435d94aaa (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
--Alexander Pickering (amp215@pitt.edu)
--
-- More moonscript!
-- $ luarocks install xavante
-- $ moon app.lua

web = require("xavante")
hw5 = require("hw5")

pages = {}
add_page = (pat, fun) -> table.insert(pages, {match: pat, with: fun})

hello = (req, res, indexname) ->
  res.content = "<html><h1>Hello, world!</h1></html>"
  res

add_page("^/$",hello)

decode_entity = (s) ->
  --decode the number using base 16, then convert the number to a string
  string.char(tonumber(s,16))

proper = (req, res, indexname) ->
  query = req.parsed_url.query
  --Decode the %20 entties in the string
  decoded = string.gsub(query,"%%(%d+)",decode_entity)
  mixed = hw5(decoded)
  --Poor man's json
  quoted = [string.format("%q",word) for word in *mixed]
  res.content = "[" .. table.concat(quoted,",") .. "]"
  res

add_page("^/proper$",proper)

config = {}
config.server =
  host: "*"
  port: 8080

config.defaultHost = {rules: pages}

web.HTTP(config)
web.start!