-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab48c04
commit 0c84abb
Showing
10 changed files
with
298 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
local Router = require('resty.router') | ||
|
||
local router = Router:new() | ||
|
||
-- 测试插件 | ||
router:use(function(ctx) | ||
ctx.plugin_executed = true | ||
ctx:yield() | ||
end) | ||
|
||
-- 测试事件 | ||
router:on('success', function(ctx) | ||
ngx.log(ngx.INFO, "请求成功") | ||
end) | ||
|
||
router:on('error', function(ctx) | ||
ngx.log(ngx.INFO, "请求失败") | ||
end) | ||
|
||
-- 1. 测试静态路径 | ||
router:get("/hello", function() | ||
return "Hello World" | ||
end) | ||
|
||
-- 2. 测试JSON响应 | ||
router:get("/json", function() | ||
return { message = "success", code = 0 } | ||
end) | ||
|
||
-- 3. 测试动态路径参数 | ||
router:get("/users/#id", function(ctx) | ||
return { | ||
id = ctx.params.id, | ||
type = "number" | ||
} | ||
end) | ||
|
||
router:get("/users/:name", function(ctx) | ||
return { | ||
name = ctx.params.name, | ||
type = "string" | ||
} | ||
end) | ||
|
||
-- 4. 测试正则路径 | ||
router:get("/version/<ver>\\d+\\.\\d+", function(ctx) | ||
return { | ||
version = ctx.params.ver | ||
} | ||
end) | ||
|
||
-- 5. 测试通配符 | ||
router:get("/files/*path", function(ctx) | ||
return { | ||
path = ctx.params.path | ||
} | ||
end) | ||
|
||
-- 6. 测试多个HTTP方法 | ||
router:post("/users", function(ctx) | ||
return { method = "POST" } | ||
end) | ||
|
||
router:put("/users/#id", function(ctx) | ||
return { method = "PUT", id = ctx.params.id } | ||
end) | ||
|
||
-- 7. 测试错误处理 | ||
router:get("/error", function() | ||
error("测试错误") | ||
end) | ||
|
||
-- 8. 测试状态码 | ||
router:get("/404", function() | ||
return nil, "Not Found", 404 | ||
end) | ||
|
||
-- 9. 测试HTML响应 | ||
router:get("/html", function() | ||
return "<h1>Hello HTML</h1>" | ||
end) | ||
|
||
-- 10. 测试函数返回 | ||
router:get("/func", function() | ||
return function() | ||
ngx.say("function called") | ||
return true | ||
end | ||
end) | ||
|
||
return router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
worker_processes auto; | ||
pid logs/nginx.pid; | ||
|
||
error_log logs/error.log; | ||
events { | ||
worker_connections 1024; | ||
} | ||
|
||
http { | ||
default_type application/json; | ||
access_log logs/access.log; | ||
|
||
lua_package_path './lib/?.lua;;'; | ||
lua_code_cache on; | ||
uwsgi_temp_path /tmp; | ||
fastcgi_temp_path /tmp; | ||
client_body_temp_path /tmp; | ||
proxy_temp_path /tmp; | ||
scgi_temp_path /tmp; | ||
server { | ||
listen 8080; | ||
server_name localhost; | ||
charset utf-8; | ||
|
||
location / { | ||
content_by_lua_block { | ||
require("app"):run() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
local Router = require('resty.router') | ||
|
||
local router = Router:new() | ||
|
||
router:get('/', function(ctx) | ||
ctx.response.status = 200 | ||
ctx.response.body = 'Hello, World!' | ||
end) | ||
|
||
return router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.