Mercurial Hosting > freedit
diff src/api/Api.luan @ 6:9166f6a14021
add email api
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 23 Jun 2022 23:05:28 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/api/Api.luan Thu Jun 23 23:05:28 2022 -0600 @@ -0,0 +1,37 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local new_error = Luan.new_error or error() +local Parsers = require "luan:Parsers.luan" +local json_string = Parsers.json_string or error() +local Io = require "luan:Io.luan" +local Http = require "luan:http/Http.luan" + + +local Api = {} + +function Api.api(fn) + return function() + Io.stdout = Http.response.text_writer() + try + local rtn = fn() + %><%=json_string(rtn)%><% + catch e + if e.error ~= nil then + %><%=json_string(e.error)%><% + return + end + e.throw() + end + end +end + +function Api.user_error(msg) + local e = new_error(msg) + e.error = { + okay = false + error = msg + } + e.throw() +end + +return Api