lapis http verb 处理
// 路由格式 match ,通过respond_to 进行实际的http verb 处理
local lapis = require("lapis")
local respond_to = require("lapis.application").respond_to
local app = lapis.Application()
app:match("create_account", "/create-account", respond_to({
GET = function(self)
return { render = true }
end,
POST = function(self)
do_something(self.params)
return { redirect_to = self:url_for("index") }
end
}))
备注 respond_to 可以包含一个before 的filter 进行一个格外的权限,参数处理
如下:
app:match("edit_user", "/edit-user/:id", respond_to({
before = function(self)
self.user = Users:find(self.params.id)
if not self.user then
self:write({"Not Found", status = 404})
end
end,
GET = function(self)
return "Edit account " .. self.user.name
end,
POST = function(self)
self.user:update(self.params.user)
return { redirect_to = self:url_for("index") }
end
}))
如下:
app:get("/test", function(self)
return "I only render for GET requests"
end)
app:delete("/delete-account", function(self)
-- do something destructive
end)
// 全局控制
app:before_filter(function(self)
if not user_meets_requirements() then
self:write({redirect_to = self:url_for("login")})
end
end)
// respond_to 添加,例子参考上面的
比较多,参考文档 http://leafo.net/lapis/reference/actions.html#routes-and-url-patterns/route-precedence
主要是一些 session cookies url 处理
参考格式
app:match("/", function(self)
return { render = "error", status = 404}
end)
参数选项
status — http 状态码
render — 渲染的模板名称
content_type — 内容类型
headers — http header 参数
json — 指定返回的数据是json 格式的,既 content_type application/json
layout — 指定渲染使用的模板,可以全局设置
redirect_to — 设置重定向类型 301 302 。。。
function app:default_route()
ngx.log(ngx.NOTICE, "User hit unknown path " .. self.req.parsed_url.path)
-- call the original implementaiton to preserve the functionality it provides
return lapis.Application.default_route(self)
end
function app:handle_error(err, trace)
if config.custom_error_page then
return { render = "my_custom_error_page" }
else
return lapis.Application.handle_error(self, err, trace)
end
end
http://leafo.net/lapis/reference/actions.html#routes-and-url-patterns/route-precedence
https://github.com/leafo/lapis-exceptions
lapis http verb 处理的更多相关文章
- luarocks install with lua5.1 and luajit to install lapis
# in luarocks source directory...git clone https://github.com/archoncap/luarockscd luarocks ./config ...
- lapis 项目添加prometheus 监控集成grafana
操作很简单,主要是进行界面的配置以及prometheus 服务的配置, 可以和https://www.cnblogs.com/rongfengliang/p/10074044.html &&a ...
- lapis 项目添加prometheus 监控
lapis 是基于openresty 扩展的,所以直接将支持prometheus的模块构建进openresty 就可以了 我使用的是nginx-module-vts 模块 环境准备 我已经构建好了 ...
- lapis 1.7.0 更好的openresty 版本兼容以及安全数据库支持
lapis 1.7.0 今年4月2号就发布了,一直没有注意,今天看到changelog就简单的进行了一个 测试(主要是与openresty版本的测试,新变更后边会有) 使用docker-compose ...
- what is HTTP OPTIONS verb
The options verb is sent by browser to see if server accept cross origin request or not, this proces ...
- lapis docker 运行说明
1. lapis docker 镜像制作 因为openresty 新版本一个json 库的问题,我们使用的是 openresty:1.11.2.1 基础镜像 FROM openresty/openre ...
- lapis 处理接收到的json 数据
备注: 在restful api 开发过程中,大家一般使用的都是json 格式的数据lapis 在处理json 数据上也是比较方便的 1. 使用的api 说明 local ...
- luarocks yum 安装引起的lapis lua 包查找问题(centos7版本)
备注: 大家在进行lapis 开发的时候有些人比较懒直接使用yum 按照luarocks,之后 使用luarocks 安装lapis 一般来说对于linux 64位的环境都会有些问题(包找 ...
- lapis 集成openresty最新版本cjson 问题的解决
备注: 为了解决安装了lapis.同时又希望使用新版nginx 以及openresty 的特性(stream ...) 1. 解决方法 参考: https://github.com/leaf ...
随机推荐
- nginx 日志搜集解决方案
# nginx 日志搜集解决方案 ## 系统环境描述 ``` java8 logstash --监控nginx日志文件 ``` ## 技术描述 ``` 通过logstash监控nginx access ...
- CentOS开端口问题
关闭SELINUX ##查看SELINUX状态 /usr/sbin/sestatus -v getenforce #修改config配置文件,重启后即可 vi /etc/selinux/config ...
- 51NOD-1960-数学/贪心
1960 范德蒙矩阵 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 LYK最近在研究范德蒙矩阵与矩阵乘法,一个范德蒙矩阵的形式如下: 它想通过构 ...
- vue基础指令
- I/O复用服务器端+回声客户端
并发服务器的第二种实现方法:I/O复用 服务器端: #include <arpa/inet.h> #include <unistd.h> #include <algori ...
- C#_串口通信_SerialPort_一个最基础的串口程序
一个最最基础的 串口通信 程序!!! 最近正在学c#_还不是很熟悉_只是有点java的基础 SerialPort类 的介绍 http://msdn.microsoft.com/zh-cn/librar ...
- Hrbust 1535 相爱
Description 静竹在斐波那契的帮助下,渐渐的和数学相爱了.和数学在一起最有意思的就是它能够出一些特别有意思并且巧妙的题目让静竹来思考.这次也不例外,给静竹两个数a,b,又给出了加,减,乘,除 ...
- cookie注入原理及注入检测
通常我们的开发人员在开发过程中会特别注意到防止恶意用户进行恶意的注入操作,因此会对传入的参数进行适当的过滤,但是很多时候,由于个人对安全技术了解的不同,有些开发人员只会对get,post这种方式提交的 ...
- functools 和 itertools
functools 补充 1 wraps 在编写装饰器时,在实现前加入 @functools.wraps(func) 可以保证装饰器不会对被装饰函数造成影响.wraps 保存被装饰函数的原信息 def ...
- C++解决error C4996报错
今天用c++写了个数独程序,在编译过程中报了一个错误: 1>------ 已启动生成: 项目: sudoku, 配置: Debug Win32 ------1> main.cpp1> ...