summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-04-26 13:26:44 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-04-26 14:18:06 +0300
commitdede4d8f53660a0fc41f3143b5d0727f45cc7e57 (patch)
tree72ae1f712e0adff2ac5bccdd095739a0b6bcc3a9
parent54e232e9756f638089355c99dd1ad05b2fa2a6ea (diff)
downloadaconf-dede4d8f53660a0fc41f3143b5d0727f45cc7e57.tar.bz2
aconf-dede4d8f53660a0fc41f3143b5d0727f45cc7e57.tar.xz
use JSON encoding also for primitive values in response
-rw-r--r--server.lua25
1 files changed, 12 insertions, 13 deletions
diff --git a/server.lua b/server.lua
index 8ad6d88..9fe4f5e 100644
--- a/server.lua
+++ b/server.lua
@@ -22,18 +22,17 @@ return function(env)
local method = env.REQUEST_METHOD
local path = env.REQUEST_URI
- local function wrap(code, headers, res)
+ local function wrap(code, headers, res, encode)
if not headers then headers = {} end
- local ctype
- if type(res) == 'table' then
- ctype = 'application/json'
- res = json.encode(res)
- else
- ctype = 'text/plain'
- if not res then res = '' end
- end
- headers['Content-Type'] = ctype
+ if res then
+ local ctype
+ if encode then
+ ctype = 'application/json'
+ res = json.encode(res)
+ else ctype = 'text/plain' end
+ headers['Content-Type'] = ctype
+ else res = '' end
return code, headers, coroutine.wrap(
function() coroutine.yield(res) end
@@ -225,9 +224,9 @@ return function(env)
end
)
- if success then return wrap(code, hdr, res) end
+ if success then return wrap(code, hdr, res, true) end
- if code.conflict then return wrap(409, nil, code.conflict) end
+ if code.conflict then return wrap(409, nil, code.conflict, true) end
- return wrap(422, nil, code)
+ return wrap(422, nil, code, true)
end