• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

APItools/router.lua: A barebones router for Lua. It matches urls and executes lu ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

APItools/router.lua

开源软件地址(OpenSource Url):

https://github.com/APItools/router.lua

开源编程语言(OpenSource Language):

Lua 100.0%

开源软件介绍(OpenSource Introduction):

router.lua

Build Status

A very basic router for lua.

Features:

  • Allows binding a method and a path to a function
  • Parses parameters like /app/services/:service_id
  • It's platform-agnostic. It has been tested with openresty.

Usage

A router is created with router.new:

local router = require 'router'
local r = router.new()

You can define a route with r:match:

local router = require 'router'
local r = router.new()

r:match('GET', '/hello', function(params)
  print('someone said hello')
end)

You can use r:get(...) instead of r:match('GET', ...). There are similar shortcuts for the usual http verbs (r:post, r:put, r:delete ...).

If you have a route which can handle multiple methods, you can use r:any(...), which will match any http verb. This also passes the verb as a second parameter to the function:

local router = require 'router'
local r = router.new()

r:any('/hello', function(params, method)
  print('someone said hello using ' .. method)
end)

In addition to that, router.lua supports router parameters (like /users/:id/comment) and extra parameters (which come from outside the route).

local router = require 'router'
local r = router.new()

r:get('/hello', function(params)
  print('someone said hello')
end)

-- route parameters
r:get('/hello/:name', function(params)
  print('hello, ' .. params.name)
end)

-- extra parameters (i.e. from a query or form)
r:post('/app/:id/comments', function(params)
  print('comment ' .. params.comment .. ' created on app ' .. params.id)
end)

Once the routes are defined, you can trigger their actions by using r:execute. Given the 3 routes above, execute will work like this:

r:execute('GET',  '/hello')
-- prints "someone said hello"

r:execute('GET',  '/hello/peter')
-- prints "hello peter"

r:execute('POST', '/app/4/comments', { comment = 'fascinating'})
-- prints "comment fascinating created on app 4"

r:execute returns either nil followed by an error message if no routes where found, or true and whatever the matched action returned.

If you are defining lots of routes in one go, there is an extra-compact syntax to do so using a table. The following code is equivalent to the previous one:

local router = require 'router'
local r = router.new()

r:match({
  GET = {
    ['/hello']       = function(params) print('someone said hello') end,
    ['/hello/:name'] = function(params) print('hello, ' .. params.name) end
  },
  POST = {
    ['/app/:id/comments'] = function(params)
      print('comment ' .. params.comment .. ' created on app ' .. params.id)
    end
  }
})

r:execute('GET',  '/hello')
r:execute('GET',  '/hello/peter')
r:execute('POST', '/app/4/comments', { comment = 'fascinating'})

Usage with openresty

router.lua is platform-agnostic, but you can use it with openresty like this:

# nginx.conf
http {
  server {
    listen 80;

    location / {
      content_by_lua '
      local router = require 'router'
      local r = router.new()

      r:match({
        GET = {
          ["/hello"]       = function(params) ngx.print("someone said hello") end,
          ["/hello/:name"] = function(params) ngx.print("hello, " .. params.name) end
        },
        POST = {
          ["/app/:id/comments"] = function(params)
            ngx.print("comment " .. params.comment .. " created on app " .. params.id)
          end
        }
      })

      local ok, errmsg = r:execute(
        ngx.var.request_method,
        ngx.var.request_uri,
        ngx.req.get_uri_args(),  -- all these parameters
        ngx.req.get_post_args(), -- will be merged in order
        {other_arg = 1})         -- into a single "params" table

      if ok then
        ngx.status = 200
      else
        ngx.status = 404
        ngx.print("Not found!")
        ngx.log(ngx.ERROR, errmsg)
      end
    }
  }

Read more about it in https://docs.apitools.com/blog/2014/04/24/a-small-router-for-openresty.html

Testing

Install dependencies:

luarocks install luacheck
luarocks install busted
luarocks install luacov
luarocks install luacov-coveralls

And run tests:

luacheck --std max+busted *.lua spec
busted --verbose --coverage --lpath=./?.lua

License

MIT license

Specs

This library uses busted for its specs. In order to run the specs, install busted and then do

cd path/to/the/folder/where/the/spec/folder/is
busted



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap