1. 同一个url 包含不同的请求(respond_to  进行解决)
// 路由格式 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
}))
2. 明确指明请求的http verb
如下:

app:get("/test", function(self)
return "I only render for GET requests"
end) app:delete("/delete-account", function(self)
-- do something destructive
end)
3. http verb 请求的过滤(before filter )
// 全局控制
app:before_filter(function(self)
if not user_meets_requirements() then
self:write({redirect_to = self:url_for("login")})
end
end)
// respond_to 添加,例子参考上面的
4. http  request && response API 参考
比较多,参考文档 http://leafo.net/lapis/reference/actions.html#routes-and-url-patterns/route-precedence
主要是一些 session cookies url 处理
5. http  render 参数
参考格式
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 。。。
5. 默认请求处理 (默认路由)
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
6. 异常错误处理
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
7. 参考文档
http://leafo.net/lapis/reference/actions.html#routes-and-url-patterns/route-precedence
https://github.com/leafo/lapis-exceptions
 
 
 
 

lapis http verb 处理的更多相关文章

  1. luarocks install with lua5.1 and luajit to install lapis

    # in luarocks source directory...git clone https://github.com/archoncap/luarockscd luarocks ./config ...

  2. lapis 项目添加prometheus 监控集成grafana

    操作很简单,主要是进行界面的配置以及prometheus 服务的配置, 可以和https://www.cnblogs.com/rongfengliang/p/10074044.html &&a ...

  3. lapis 项目添加prometheus 监控

      lapis 是基于openresty 扩展的,所以直接将支持prometheus的模块构建进openresty 就可以了 我使用的是nginx-module-vts 模块 环境准备 我已经构建好了 ...

  4. lapis 1.7.0 更好的openresty 版本兼容以及安全数据库支持

    lapis 1.7.0 今年4月2号就发布了,一直没有注意,今天看到changelog就简单的进行了一个 测试(主要是与openresty版本的测试,新变更后边会有) 使用docker-compose ...

  5. what is HTTP OPTIONS verb

    The options verb is sent by browser to see if server accept cross origin request or not, this proces ...

  6. lapis docker 运行说明

    1. lapis docker 镜像制作 因为openresty 新版本一个json 库的问题,我们使用的是 openresty:1.11.2.1 基础镜像 FROM openresty/openre ...

  7. lapis 处理接收到的json 数据

     备注:      在restful api 开发过程中,大家一般使用的都是json 格式的数据lapis       在处理json 数据上也是比较方便的   1. 使用的api 说明 local ...

  8. luarocks yum 安装引起的lapis lua 包查找问题(centos7版本)

    备注:     大家在进行lapis 开发的时候有些人比较懒直接使用yum 按照luarocks,之后   使用luarocks 安装lapis 一般来说对于linux 64位的环境都会有些问题(包找 ...

  9. lapis 集成openresty最新版本cjson 问题的解决

    备注:    为了解决安装了lapis.同时又希望使用新版nginx 以及openresty 的特性(stream ...)   1. 解决方法 参考: https://github.com/leaf ...

随机推荐

  1. 关系型数据库(RDBMS)与 MongoDB 的对应关系

    谈一下关系型数据库(RDBMS)与 MongoDB 的对应关系:

  2. 终于搞懂了shell bash cmd...

    问题一:DOS与windows中cmd区别 在windows系统中,“开始-运行-cmd”可以打开“cmd.exe”,进行命令行操作. 操作系统可以分成核心(kernel)和Shell(外壳)两部分, ...

  3. 负载均衡,会话保持,session同步

    一,什么负载均衡 一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情 ...

  4. vim 删除多列

    比如想删除一个文件的1-1000行的前3列: gg #到文件首ctrl+v #可视块模式999j,2l #注意是小写的J和L(方向键向下和向右也可以)d #删除

  5. SpringBoot下的值注入

    在我们实际开发项目中,经常会遇到一些常量的配置,比如url,暂时不会改变的字段参数,这个时候我们最好是不要直接写死在代码里的,因为这样编写的程序,应用扩展性太差了,我们可以直接写在配置文件中然后通过配 ...

  6. 翻译"Python编程无师自通——专业程序员的养成"

    这本书在 畅销Python编程类入门书,美国亚马逊Kindle编程类排行榜榜一. 开始初学python,也有不少书了,不想在白花钱(买了就放那里不看了),看一个英文文档的原著,准备每天翻译一点,放到b ...

  7. magento: Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside 解决方案

    在linux(以UBUNTU, CENTOS为例)下安装完成magento时,在进入后台时, 有些童鞋可能会发现有如下的提示: Your web server is configured incorr ...

  8. java语言基础-变量

    一丶变量的基本概念 1.什么是变量 (1).内存中的一个存储区域 (2).该区域有自己的名称(变量名),和类型(数据类型) (3.)该区域的数据可以在同一类型范围内不断变化(定义变量的主要目的是因为数 ...

  9. Spring报错: org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'init' on bean with name 'car'(待解答)

    在Spring工程里,有一个Car类的bean,Main.java主程序,MyBeanPostProcessor.java是Bean后置处理器. 文件目录结构如下: Car.java package ...

  10. PostgreSQL时间格式及相关函数实践

    在创建表的时候,有客户需要将时间转为字符串,而且要求了具体的格式,如:20181115101010001.方便记录数据的更新时间,貌似是给Mysql使用,当时就很蛋疼,时间格式存储子啊数据库中就是va ...