Mercurial Hosting > freedit
comparison 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 |
comparison
equal
deleted
inserted
replaced
5:2f20b11affdd | 6:9166f6a14021 |
---|---|
1 local Luan = require "luan:Luan.luan" | |
2 local error = Luan.error | |
3 local new_error = Luan.new_error or error() | |
4 local Parsers = require "luan:Parsers.luan" | |
5 local json_string = Parsers.json_string or error() | |
6 local Io = require "luan:Io.luan" | |
7 local Http = require "luan:http/Http.luan" | |
8 | |
9 | |
10 local Api = {} | |
11 | |
12 function Api.api(fn) | |
13 return function() | |
14 Io.stdout = Http.response.text_writer() | |
15 try | |
16 local rtn = fn() | |
17 %><%=json_string(rtn)%><% | |
18 catch e | |
19 if e.error ~= nil then | |
20 %><%=json_string(e.error)%><% | |
21 return | |
22 end | |
23 e.throw() | |
24 end | |
25 end | |
26 end | |
27 | |
28 function Api.user_error(msg) | |
29 local e = new_error(msg) | |
30 e.error = { | |
31 okay = false | |
32 error = msg | |
33 } | |
34 e.throw() | |
35 end | |
36 | |
37 return Api |