在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):APItools/router.lua开源软件地址(OpenSource Url):https://github.com/APItools/router.lua开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):router.luaA very basic router for lua. Features:
UsageA router is created with local router = require 'router'
local r = router.new() You can define a route with local router = require 'router'
local r = router.new()
r:match('GET', '/hello', function(params)
print('someone said hello')
end) You can use If you have a route which can handle multiple methods, you can use 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, 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('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"
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
Read more about it in https://docs.apitools.com/blog/2014/04/24/a-small-router-for-openresty.html TestingInstall 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 LicenseMIT license SpecsThis library uses busted for its specs. In order to run the specs, install
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论