Skip to content

Commit 00a853a

Browse files
committed
to:npm
1 parent e0c2426 commit 00a853a

File tree

8 files changed

+463
-268
lines changed

8 files changed

+463
-268
lines changed

.github/workflows/publish-package-github.yml

-49
This file was deleted.

README.md

+135-60
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
Elegant, performant and productive router for Openresty.
44

5-
# Router
5+
# Quick Start
6+
7+
```bash
8+
npm create lua-resty-router@latest
9+
```
10+
11+
# Api
612

713
## create
814

@@ -37,68 +43,137 @@ match a http request
3743

3844
```lua
3945
local Router = require('resty.router')
40-
local tree = Router:create {
41-
{ '/', 'root', { 'GET', 'patch' } },
42-
{ '/v1', 'v1', 'GET' },
43-
{ '/number/#age', 'age', 'GET' },
44-
{ '/all/:name', 'name', 'GET' },
45-
{ '/regex/<version>\\d+-\\d+', 'version', 'GET' },
46-
{ '/name', 'name', 'GET' },
47-
{ '/name/:name/age/#age', 'person', 'GET' },
48-
{ '/v2', 'v2', 'post' },
49-
{ '/repo/:repo/path/*path', 'rest', 'get' },
50-
}
51-
52-
local res, err, status = tree:match('/', 'POST')
53-
assert(res == nil and err == 'method not allowed' and status == 405)
54-
local res, params, status = tree:match('/v2/foo', 'GET')
55-
assert(res == nil and params == 'page not found' and status == 404)
56-
assert(tree:match('/', 'GET') == tree:match('/', 'PATCH'))
57-
assert(tree:match('/v1', 'GET') == 'v1')
58-
local res, params = tree:match('/number/#age', 'GET')
59-
assert(res == 'age')
60-
assert(params == nil)
61-
local res, params = tree:match('/number/23', 'GET')
62-
assert(res == 'age', 'res is :' .. tostring(res))
63-
assert(params.age == 23)
64-
local res, params = tree:match('/regex/1-3', 'GET')
65-
assert(res == 'version')
66-
assert(params.version == '1-3')
67-
local res, params = tree:match('/all/kate', 'GET')
68-
assert(res == 'name')
69-
assert(params.name == 'kate')
70-
local res, params = tree:match('/name/kate/age/23', 'GET')
71-
assert(res == 'person')
72-
assert(params.name == 'kate')
73-
assert(params.age == 23)
74-
assert(tree:match('/name', 'GET') == 'name')
75-
76-
local res, params = tree:match('/repo/my_repo/path/lib/resty/router.lua', 'GET')
77-
assert(res == 'rest')
78-
assert(params.repo == 'my_repo')
79-
assert(params.path == '/lib/resty/router.lua')
80-
81-
local tree = Router:new()
82-
tree:insert('/v1', 'v1')
83-
tree:post('/v2', 'v2')
84-
assert(tree:match('/v1', 'GET') == 'v1')
85-
assert(tree:match('/v2', 'POST') == 'v2')
86-
assert(tree:match('/v2', 'GET') == nil)
87-
88-
print('all tests passed')
89-
```
9046

91-
Or like this:
47+
local router = Router:new()
48+
49+
local cnt = 0
50+
local error_cnt = 0
51+
52+
-- Test events
53+
router:on('add', function(ctx)
54+
cnt = cnt + 1
55+
end)
56+
57+
router:on('error', function(ctx)
58+
error_cnt = error_cnt + 1
59+
end)
60+
61+
-- Test plugins
62+
router:use(function(ctx)
63+
ctx.cnt = cnt
64+
ctx.error_cnt = error_cnt
65+
end)
66+
67+
-- 1. Test static path
68+
router:get("/hello", function()
69+
return "Hello World"
70+
end)
71+
72+
-- Test custom success status code
73+
router:get("/hello-201", function()
74+
return "Hello World", 201
75+
end)
76+
77+
-- 2. Test JSON response
78+
router:get("/json", function()
79+
return { message = "success", code = 0 }
80+
end)
81+
82+
-- 3. Test dynamic path parameters
83+
router:get("/users/#id", function(ctx)
84+
return {
85+
id = ctx.params.id,
86+
type = type(ctx.params.id)
87+
}
88+
end)
89+
90+
router:get("/users/:name", function(ctx)
91+
return {
92+
name = ctx.params.name,
93+
type = type(ctx.params.name)
94+
}
95+
end)
96+
97+
-- 4. Test regex path
98+
router:get([[/version/<ver>\d+\.\d+]], function(ctx)
99+
return {
100+
version = ctx.params.ver
101+
}
102+
end)
103+
104+
-- 5. Test wildcard
105+
router:get("/files/*path", function(ctx)
106+
return ctx.params.path
107+
end)
108+
109+
-- 6. Test other HTTP methods
110+
router:post("/accounts", function(ctx)
111+
return { method = "POST" }
112+
end)
113+
114+
router:put("/accounts/#id", function(ctx)
115+
return { method = "PUT", id = ctx.params.id }
116+
end)
117+
118+
-- 7. Test error handling
119+
router:get("/error", function()
120+
error("Test Error")
121+
end)
122+
123+
-- Test custom error thrown by error()
124+
router:get("/custom-error", function()
125+
error({ code = 400, message = "Custom Error" })
126+
end)
127+
128+
-- Test error in the form of return nil, err, code
129+
router:get("/return-error", function()
130+
return nil, "Parameter Error", 402
131+
end)
132+
133+
-- Test handled error
134+
router:get("/handled-error", function()
135+
error { "handled error" }
136+
end)
137+
138+
-- 8. Test status code
139+
router:get("/404", function()
140+
return nil, "Not Found", 404
141+
end)
142+
143+
-- 9. Test HTML response
144+
router:get("/html", function()
145+
return "<h1>Hello HTML</h1>"
146+
end)
147+
-- Test HTML error
148+
router:get("/html-error", function()
149+
return nil, "<h1>Hello HTML error</h1>", 501
150+
end)
151+
-- Test HTML error thrown by error()
152+
router:get("/html-error2", function()
153+
error { "<h1>Hello HTML error2</h1>" }
154+
end)
155+
156+
-- 10. Test function return
157+
router:get("/func", function(ctx)
158+
return function()
159+
ngx.header.content_type = 'text/plain; charset=utf-8'
160+
ngx.say("function called2")
161+
end
162+
end)
163+
164+
-- 11. Check if events are executing properly
165+
router:get("/add", function(ctx)
166+
cnt = cnt + 1
167+
return cnt
168+
end)
169+
170+
router:get("/events", function(ctx)
171+
return ctx.cnt
172+
end)
173+
174+
return router
92175

93-
```lua
94-
local tree = Router:new()
95-
tree:insert('/v1', 'v1')
96-
tree:post('/v2', 'v2')
97-
assert(tree:match('/v1','GET') == 'v1')
98-
assert(tree:match('/v2','POST') == 'v2')
99-
assert(tree:match('/v2','GET') == nil)
100176
```
101-
102177
## reference
103178

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

0 commit comments

Comments
 (0)