docs(lua): 添加 Lua 配置示例和示例脚本
- lua-example.yaml 展示完整 Lua 中间件配置 - examples/lua-scripts/ 包含 auth.lua、content.lua、log.lua 示例 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
153982121e
commit
6a6cfcd11c
63
docs/config/lua-example.yaml
Normal file
63
docs/config/lua-example.yaml
Normal file
@ -0,0 +1,63 @@
|
||||
# Lua Middleware 示例配置
|
||||
# 此文件演示如何在 lolly 中配置 Lua 中间件
|
||||
|
||||
server:
|
||||
listen: ":8080"
|
||||
name: "lua-demo"
|
||||
|
||||
# Lua 中间件配置
|
||||
lua:
|
||||
enabled: true
|
||||
|
||||
# 全局 Lua 引擎设置
|
||||
global_settings:
|
||||
max_concurrent_coroutines: 1000 # 最大并发协程数
|
||||
coroutine_timeout: 30s # 协程执行超时
|
||||
code_cache_size: 1000 # 字节码缓存条目数
|
||||
enable_file_watch: true # 启用文件变更检测(热重载)
|
||||
max_execution_time: 30s # 单脚本最大执行时间
|
||||
|
||||
# 脚本配置列表
|
||||
scripts:
|
||||
# Access 阶段 - 认证检查
|
||||
- path: "/etc/lolly/scripts/auth.lua"
|
||||
phase: "access"
|
||||
timeout: 10s
|
||||
enabled: true
|
||||
|
||||
# Content 阶段 - 内容生成
|
||||
- path: "/etc/lolly/scripts/content.lua"
|
||||
phase: "content"
|
||||
timeout: 30s
|
||||
enabled: true
|
||||
|
||||
# Log 阶段 - 日志记录
|
||||
- path: "/etc/lolly/scripts/log.lua"
|
||||
phase: "log"
|
||||
timeout: 5s
|
||||
enabled: true
|
||||
|
||||
# 其他服务器配置
|
||||
read_timeout: 30s
|
||||
write_timeout: 30s
|
||||
idle_timeout: 120s
|
||||
max_conns_per_ip: 100
|
||||
max_requests_per_conn: 1000
|
||||
|
||||
# 性能配置
|
||||
performance:
|
||||
goroutine_pool:
|
||||
enabled: true
|
||||
max_workers: 1000
|
||||
min_workers: 100
|
||||
idle_timeout: 60s
|
||||
|
||||
# 日志配置
|
||||
logging:
|
||||
format: "json"
|
||||
access:
|
||||
path: "/var/log/lolly/access.log"
|
||||
format: "combined"
|
||||
error:
|
||||
path: "/var/log/lolly/error.log"
|
||||
level: "info"
|
||||
31
examples/lua-scripts/auth.lua
Normal file
31
examples/lua-scripts/auth.lua
Normal file
@ -0,0 +1,31 @@
|
||||
-- auth.lua - Access 阶段认证检查示例
|
||||
-- 此脚本演示如何在 access 阶段进行简单的 token 认证
|
||||
|
||||
local auth_header = ngx.req.get_headers()["Authorization"]
|
||||
|
||||
if not auth_header then
|
||||
ngx.say("Missing Authorization header")
|
||||
ngx.exit(401)
|
||||
end
|
||||
|
||||
-- 验证 token (示例:简单的 token 检查)
|
||||
local token = auth_header
|
||||
if string.sub(token, 1, 7) ~= "Bearer " then
|
||||
ngx.say("Invalid token format")
|
||||
ngx.exit(401)
|
||||
end
|
||||
|
||||
local actual_token = string.sub(token, 8)
|
||||
|
||||
-- 这里可以连接数据库或调用认证服务验证 token
|
||||
-- 示例:简单的 token 比较
|
||||
if actual_token ~= "valid-token-123" then
|
||||
ngx.say("Invalid token")
|
||||
ngx.exit(403)
|
||||
end
|
||||
|
||||
-- 认证成功,设置用户信息到上下文
|
||||
ngx.ctx.user_id = "user-123"
|
||||
ngx.ctx.auth_time = ngx.now()
|
||||
|
||||
-- 继续处理请求
|
||||
27
examples/lua-scripts/content.lua
Normal file
27
examples/lua-scripts/content.lua
Normal file
@ -0,0 +1,27 @@
|
||||
-- content.lua - Content 阶段内容生成示例
|
||||
-- 此脚本演示如何在 content 阶段生成响应内容
|
||||
|
||||
-- 检查是否有认证信息
|
||||
local user_id = ngx.ctx.user_id
|
||||
if not user_id then
|
||||
-- 未认证,返回错误
|
||||
ngx.say("Not authenticated")
|
||||
ngx.exit(401)
|
||||
end
|
||||
|
||||
-- 生成响应内容
|
||||
ngx.say("Hello, " .. user_id .. "!")
|
||||
ngx.say("Request processed at: " .. ngx.now())
|
||||
|
||||
-- 设置响应头
|
||||
ngx.resp.set_header("X-User-Id", user_id)
|
||||
ngx.resp.set_header("X-Server", "lolly-lua")
|
||||
|
||||
-- 可以根据请求路径返回不同内容
|
||||
local uri = ngx.var.uri
|
||||
if uri == "/api/status" then
|
||||
ngx.say("Status: OK")
|
||||
end
|
||||
|
||||
-- 正常完成处理
|
||||
ngx.exit(200)
|
||||
23
examples/lua-scripts/log.lua
Normal file
23
examples/lua-scripts/log.lua
Normal file
@ -0,0 +1,23 @@
|
||||
-- log.lua - Log 阶段日志记录示例
|
||||
-- 此脚本演示如何在 log 阶段记录请求信息
|
||||
|
||||
local log_data = {
|
||||
uri = ngx.var.uri,
|
||||
method = ngx.req.get_method(),
|
||||
status = ngx.resp.get_status(),
|
||||
user_id = ngx.ctx.user_id or "anonymous",
|
||||
auth_time = ngx.ctx.auth_time or 0,
|
||||
duration = ngx.now() - ngx.ctx.start_time
|
||||
}
|
||||
|
||||
-- 输出日志信息(实际应用中可写入文件或发送到日志服务)
|
||||
ngx.log(ngx.INFO, "Request completed: " ..
|
||||
log_data.method .. " " ..
|
||||
log_data.uri .. " " ..
|
||||
"status=" .. log_data.status .. " " ..
|
||||
"user=" .. log_data.user_id .. " " ..
|
||||
"duration=" .. log_data.duration .. "s")
|
||||
|
||||
-- 记录响应大小
|
||||
local response_size = #ngx.resp.get_headers()
|
||||
ngx.log(ngx.INFO, "Response headers count: " .. response_size)
|
||||
Loading…
x
Reference in New Issue
Block a user