refactor(lua): 提取 API 注册辅助函数
新增 RegisterAPIMethods 和 RegisterUnsafeAPI 函数,简化 Lua API 批量注册逻辑。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
81e26e70ce
commit
8a533ba0ca
@ -19,8 +19,13 @@ func RegisterNgxCtxAPI(L *glua.LState, ngxTable *glua.LTable) {
|
||||
func RegisterSchedulerUnsafeCtxAPI(L *glua.LState, ngx *glua.LTable) {
|
||||
ctxTable := L.NewTable()
|
||||
mt := L.NewTable()
|
||||
mt.RawSetString("__index", L.NewFunction(luaSchedulerUnsafeCtx))
|
||||
mt.RawSetString("__newindex", L.NewFunction(luaSchedulerUnsafeCtx))
|
||||
|
||||
methods := []APIMethod{
|
||||
{Name: "__index", Func: luaSchedulerUnsafeCtx},
|
||||
{Name: "__newindex", Func: luaSchedulerUnsafeCtx},
|
||||
}
|
||||
RegisterAPIMethods(L, mt, methods)
|
||||
|
||||
L.SetMetatable(ctxTable, mt)
|
||||
ngx.RawSetString("ctx", ctxTable)
|
||||
}
|
||||
|
||||
@ -570,24 +570,17 @@ func (api *ngxReqAPI) ResetMetrics() {
|
||||
|
||||
// RegisterSchedulerUnsafeReqAPI 为 Scheduler LState 注册不安全的 ngx.req API
|
||||
func RegisterSchedulerUnsafeReqAPI(L *glua.LState, ngx *glua.LTable) {
|
||||
ngxReq := L.NewTable()
|
||||
|
||||
ngxReq.RawSetString("get_method", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("get_uri", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("set_uri", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("get_uri_args", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("set_uri_args", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("get_headers", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("set_header", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("clear_header", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("get_body_data", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
ngxReq.RawSetString("read_body", L.NewFunction(luaSchedulerUnsafeReq))
|
||||
|
||||
ngx.RawSetString("req", ngxReq)
|
||||
}
|
||||
|
||||
// luaSchedulerUnsafeReq 在 scheduler 模式下调用 ngx.req API 时返回错误
|
||||
func luaSchedulerUnsafeReq(L *glua.LState) int {
|
||||
L.RaiseError("API ngx.req not available in timer callback context")
|
||||
return 0
|
||||
methods := []string{
|
||||
"get_method",
|
||||
"get_uri",
|
||||
"set_uri",
|
||||
"get_uri_args",
|
||||
"set_uri_args",
|
||||
"get_headers",
|
||||
"set_header",
|
||||
"clear_header",
|
||||
"get_body_data",
|
||||
"read_body",
|
||||
}
|
||||
RegisterUnsafeAPI(L, ngx, "ngx.req", methods)
|
||||
}
|
||||
|
||||
@ -201,19 +201,12 @@ func (api *ngxRespAPI) SetHeader(name, value string) {
|
||||
|
||||
// RegisterSchedulerUnsafeRespAPI 为 Scheduler LState 注册不安全的 ngx.resp API
|
||||
func RegisterSchedulerUnsafeRespAPI(L *glua.LState, ngx *glua.LTable) {
|
||||
ngxResp := L.NewTable()
|
||||
|
||||
ngxResp.RawSetString("get_status", L.NewFunction(luaSchedulerUnsafeResp))
|
||||
ngxResp.RawSetString("set_status", L.NewFunction(luaSchedulerUnsafeResp))
|
||||
ngxResp.RawSetString("get_headers", L.NewFunction(luaSchedulerUnsafeResp))
|
||||
ngxResp.RawSetString("set_header", L.NewFunction(luaSchedulerUnsafeResp))
|
||||
ngxResp.RawSetString("clear_header", L.NewFunction(luaSchedulerUnsafeResp))
|
||||
|
||||
ngx.RawSetString("resp", ngxResp)
|
||||
}
|
||||
|
||||
// luaSchedulerUnsafeResp 在 scheduler 模式下调用 ngx.resp API 时返回错误
|
||||
func luaSchedulerUnsafeResp(L *glua.LState) int {
|
||||
L.RaiseError("API ngx.resp not available in timer callback context")
|
||||
return 0
|
||||
methods := []string{
|
||||
"get_status",
|
||||
"set_status",
|
||||
"get_headers",
|
||||
"set_header",
|
||||
"clear_header",
|
||||
}
|
||||
RegisterUnsafeAPI(L, ngx, "ngx.resp", methods)
|
||||
}
|
||||
|
||||
29
internal/lua/register.go
Normal file
29
internal/lua/register.go
Normal file
@ -0,0 +1,29 @@
|
||||
// Package lua 提供 Lua API 注册辅助函数
|
||||
package lua
|
||||
|
||||
import glua "github.com/yuin/gopher-lua"
|
||||
|
||||
// APIMethod 表示一个 Lua API 方法
|
||||
type APIMethod struct {
|
||||
Name string
|
||||
Func func(*glua.LState) int
|
||||
}
|
||||
|
||||
// RegisterAPIMethods 批量注册 API 方法到 Lua 表
|
||||
func RegisterAPIMethods(L *glua.LState, tbl *glua.LTable, methods []APIMethod) {
|
||||
for _, m := range methods {
|
||||
tbl.RawSetString(m.Name, L.NewFunction(m.Func))
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterUnsafeAPI 注册不可用于 timer context 的安全桩
|
||||
func RegisterUnsafeAPI(L *glua.LState, ngx *glua.LTable, apiName string, methods []string) {
|
||||
tbl := L.NewTable()
|
||||
for _, m := range methods {
|
||||
tbl.RawSetString(m, L.NewFunction(func(L *glua.LState) int {
|
||||
L.RaiseError("API %s.%s not available in timer callback context", apiName, m)
|
||||
return 0
|
||||
}))
|
||||
}
|
||||
ngx.RawSetString(apiName, tbl)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user