forked from snap-cloud/snapCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.lua
82 lines (71 loc) · 1.95 KB
/
responses.lua
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
76
77
78
79
80
81
-- Response utils
-- ==============
--
-- Written by Bernat Romagosa and Michael Ball
--
-- Copyright (C) 2019 by Bernat Romagosa and Michael Ball
--
-- This file is part of Snap Cloud.
--
-- Snap Cloud is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- Responses
jsonResponse = function (json)
return {
layout = false,
status = 200,
readyState = 4,
json = json or {}
}
end
xmlResponse = function (xml)
return {
layout = false,
status = 200,
readyState = 4,
content_type = "text/xml",
xml
}
end
okResponse = function (message)
return jsonResponse({ message = message })
end
rawResponse = function (contents)
return {
layout = false,
status = 200,
readyState = 4,
contents
}
end
errorResponse = function (err, status)
return {
layout = false,
status = status or 500,
readyState = 4,
json = { errors = {err} }
}
end
htmlPage = function (title, contents)
return {
status = 200,
readyState = 4,
'<h1>' .. title .. '</h1>' .. contents
}
end
-- OPTIONS
cors_options = function (self)
self.res.headers['access-control-allow-headers'] = 'Content-Type'
self.res.headers['access-control-allow-methods'] = 'GET, POST, DELETE, OPTIONS'
return { status = 200, layout = false }
end