Skip to content

Commit

Permalink
to:npm
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangnanscu committed Dec 4, 2024
1 parent e0c2426 commit 00a853a
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 268 deletions.
49 changes: 0 additions & 49 deletions .github/workflows/publish-package-github.yml

This file was deleted.

195 changes: 135 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

Elegant, performant and productive router for Openresty.

# Router
# Quick Start

```bash
npm create lua-resty-router@latest
```

# Api

## create

Expand Down Expand Up @@ -37,68 +43,137 @@ match a http request

```lua
local Router = require('resty.router')
local tree = Router:create {
{ '/', 'root', { 'GET', 'patch' } },
{ '/v1', 'v1', 'GET' },
{ '/number/#age', 'age', 'GET' },
{ '/all/:name', 'name', 'GET' },
{ '/regex/<version>\\d+-\\d+', 'version', 'GET' },
{ '/name', 'name', 'GET' },
{ '/name/:name/age/#age', 'person', 'GET' },
{ '/v2', 'v2', 'post' },
{ '/repo/:repo/path/*path', 'rest', 'get' },
}

local res, err, status = tree:match('/', 'POST')
assert(res == nil and err == 'method not allowed' and status == 405)
local res, params, status = tree:match('/v2/foo', 'GET')
assert(res == nil and params == 'page not found' and status == 404)
assert(tree:match('/', 'GET') == tree:match('/', 'PATCH'))
assert(tree:match('/v1', 'GET') == 'v1')
local res, params = tree:match('/number/#age', 'GET')
assert(res == 'age')
assert(params == nil)
local res, params = tree:match('/number/23', 'GET')
assert(res == 'age', 'res is :' .. tostring(res))
assert(params.age == 23)
local res, params = tree:match('/regex/1-3', 'GET')
assert(res == 'version')
assert(params.version == '1-3')
local res, params = tree:match('/all/kate', 'GET')
assert(res == 'name')
assert(params.name == 'kate')
local res, params = tree:match('/name/kate/age/23', 'GET')
assert(res == 'person')
assert(params.name == 'kate')
assert(params.age == 23)
assert(tree:match('/name', 'GET') == 'name')

local res, params = tree:match('/repo/my_repo/path/lib/resty/router.lua', 'GET')
assert(res == 'rest')
assert(params.repo == 'my_repo')
assert(params.path == '/lib/resty/router.lua')

local tree = Router:new()
tree:insert('/v1', 'v1')
tree:post('/v2', 'v2')
assert(tree:match('/v1', 'GET') == 'v1')
assert(tree:match('/v2', 'POST') == 'v2')
assert(tree:match('/v2', 'GET') == nil)

print('all tests passed')
```

Or like this:
local router = Router:new()

local cnt = 0
local error_cnt = 0

-- Test events
router:on('add', function(ctx)
cnt = cnt + 1
end)

router:on('error', function(ctx)
error_cnt = error_cnt + 1
end)

-- Test plugins
router:use(function(ctx)
ctx.cnt = cnt
ctx.error_cnt = error_cnt
end)

-- 1. Test static path
router:get("/hello", function()
return "Hello World"
end)

-- Test custom success status code
router:get("/hello-201", function()
return "Hello World", 201
end)

-- 2. Test JSON response
router:get("/json", function()
return { message = "success", code = 0 }
end)

-- 3. Test dynamic path parameters
router:get("/users/#id", function(ctx)
return {
id = ctx.params.id,
type = type(ctx.params.id)
}
end)

router:get("/users/:name", function(ctx)
return {
name = ctx.params.name,
type = type(ctx.params.name)
}
end)

-- 4. Test regex path
router:get([[/version/<ver>\d+\.\d+]], function(ctx)
return {
version = ctx.params.ver
}
end)

-- 5. Test wildcard
router:get("/files/*path", function(ctx)
return ctx.params.path
end)

-- 6. Test other HTTP methods
router:post("/accounts", function(ctx)
return { method = "POST" }
end)

router:put("/accounts/#id", function(ctx)
return { method = "PUT", id = ctx.params.id }
end)

-- 7. Test error handling
router:get("/error", function()
error("Test Error")
end)

-- Test custom error thrown by error()
router:get("/custom-error", function()
error({ code = 400, message = "Custom Error" })
end)

-- Test error in the form of return nil, err, code
router:get("/return-error", function()
return nil, "Parameter Error", 402
end)

-- Test handled error
router:get("/handled-error", function()
error { "handled error" }
end)

-- 8. Test status code
router:get("/404", function()
return nil, "Not Found", 404
end)

-- 9. Test HTML response
router:get("/html", function()
return "<h1>Hello HTML</h1>"
end)
-- Test HTML error
router:get("/html-error", function()
return nil, "<h1>Hello HTML error</h1>", 501
end)
-- Test HTML error thrown by error()
router:get("/html-error2", function()
error { "<h1>Hello HTML error2</h1>" }
end)

-- 10. Test function return
router:get("/func", function(ctx)
return function()
ngx.header.content_type = 'text/plain; charset=utf-8'
ngx.say("function called2")
end
end)

-- 11. Check if events are executing properly
router:get("/add", function(ctx)
cnt = cnt + 1
return cnt
end)

router:get("/events", function(ctx)
return ctx.cnt
end)

return router

```lua
local tree = Router:new()
tree:insert('/v1', 'v1')
tree:post('/v2', 'v2')
assert(tree:match('/v1','GET') == 'v1')
assert(tree:match('/v2','POST') == 'v2')
assert(tree:match('/v2','GET') == nil)
```

## reference

- [nginx api for lua](https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#nginx-api-for-lua)
Loading

0 comments on commit 00a853a

Please sign in to comment.