gluon-config-api: use http method OPTIONS

This commit is contained in:
lemoer 2021-08-11 15:36:55 +02:00
parent 64ed50c44a
commit 7ae6a2b7d0

View File

@ -66,6 +66,7 @@ local parts = load_parts()
entry({"v1", "config"}, call(function(http, renderer) entry({"v1", "config"}, call(function(http, renderer)
if http.request.env.REQUEST_METHOD == 'GET' then if http.request.env.REQUEST_METHOD == 'GET' then
http:header('Content-Type', 'application/json; charset=utf-8')
http:write(json.stringify(config_get(parts), true)) http:write(json.stringify(config_get(parts), true))
elseif http.request.env.REQUEST_METHOD == 'POST' then elseif http.request.env.REQUEST_METHOD == 'POST' then
local request_body = "" local request_body = ""
@ -82,6 +83,7 @@ entry({"v1", "config"}, call(function(http, renderer)
local config = json.parse(request_body) local config = json.parse(request_body)
if not config then if not config then
http:status(400, 'Bad Request') http:status(400, 'Bad Request')
http:header('Content-Type', 'application/json; charset=utf-8')
http:write('{ "status": 400, "error": "Bad JSON in Body" }\n') http:write('{ "status": 400, "error": "Bad JSON in Body" }\n')
http:close() http:close()
return return
@ -94,6 +96,7 @@ entry({"v1", "config"}, call(function(http, renderer)
if not res then if not res then
http:status(500, 'Internal Server Error.') http:status(500, 'Internal Server Error.')
http:header('Content-Type', 'application/json; charset=utf-8')
http:write('{ "status": 500, "error": "Internal UCL Parsing Failed. This should not happen at all." }\n') http:write('{ "status": 500, "error": "Internal UCL Parsing Failed. This should not happen at all." }\n')
http:close() http:close()
return return
@ -102,25 +105,30 @@ entry({"v1", "config"}, call(function(http, renderer)
res, err = parser:validate(schema_get(parts)) res, err = parser:validate(schema_get(parts))
if not res then if not res then
http:status(400, 'Bad Request') http:status(400, 'Bad Request')
http:header('Content-Type', 'application/json; charset=utf-8')
http:write('{ "status": 400, "error": "Schema mismatch" }\n') http:write('{ "status": 400, "error": "Schema mismatch" }\n')
http:close() http:close()
return return
end end
-- Apply config -- Apply config
config_set(parts, config) config_set(parts, config)
-- Write result
http:write(json.stringify(res, true)) http:write(json.stringify(res, true))
elseif http.request.env.REQUEST_METHOD == 'OPTIONS' then
local result = json.stringify({ schema = schema_get(parts)}, true)
-- Content-Length is needed, as the transfer encoding is not chunked.
http:header('Content-Length', tostring(#result))
http:header('Content-Type', 'application/json; charset=utf-8')
http:write(result)
else else
http:status(400, 'Bad Request') http:status(400, 'Bad Request')
http:header('Content-Length', '0')
http:write('Not implemented') http:write('Not implemented')
end end
http:close() http:close()
end)) end))
entry({"v1", "schema"}, call(function(http, renderer)
http:write(json.stringify(schema_get(parts), true))
http:close()
end))