Skip to content

Instantly share code, notes, and snippets.

@kurapica
Created July 12, 2021 13:22
Show Gist options
  • Save kurapica/d82ee5353815b5b77488fcf5670d0b07 to your computer and use it in GitHub Desktop.
Save kurapica/d82ee5353815b5b77488fcf5670d0b07 to your computer and use it in GitHub Desktop.
Controller Example
class "FileController" (function(_ENV)
inherit "Controller"
-- GET /file/download?name=test&fields=["name","age"]
__Action__("download", HttpMethod.GET)
__Form__{ -- Form验证并自动按照结构组织数据,下面的fields是字符串数组
name = String,
fields = struct { String },
}
__File__() __Iterator__() -- 申明返回为文件,并且使用迭代器的形式返回数据
-- 验证用户的菜单访问权限,这里仅用于展示,验证失败返回401
__Resource__(ServiceType.MENU, MENU.FileDownload, ResourcePermission.READ)
-- 验证用户的文件访问权限,可以做多组权限验证
__Resource__(ServiceType.FILE, ResourcePermission.READ)
function download_table(self, context, form, err)
if err then return self:NotFound() end -- 404
-- 返回文件名
yield( (form.name or "test") .. ".csv" )
-- 返回内容
for _, v in ipairs(form.fields) do
yield(v)
yield(n)
end
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment