refactor(lua): 提取常量并优化结构体字段布局
- api_var.go: 提取 argPrefix 常量,消除魔法字符串 - coroutine.go: 按逻辑分组重排 LuaCoroutine 字段,改善可读性 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4000d220f4
commit
9d95aecd6f
@ -8,6 +8,9 @@ import (
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
// argPrefix 是 arg_ 变量的前缀,用于获取查询参数
|
||||
const argPrefix = "arg_"
|
||||
|
||||
// ngxVarAPI ngx.var API 实现
|
||||
type ngxVarAPI struct {
|
||||
// 请求上下文
|
||||
@ -166,12 +169,12 @@ func (api *ngxVarAPI) getVariableLua(name string) glua.LValue {
|
||||
return glua.LString(string(api.ctx.Host()))
|
||||
|
||||
// URI 参数
|
||||
case "arg_":
|
||||
case argPrefix:
|
||||
// 获取所有参数
|
||||
return glua.LString(string(api.ctx.URI().QueryString()))
|
||||
default:
|
||||
// 检查是否是 arg_ 开头的参数
|
||||
if len(name) > 4 && name[:4] == "arg_" {
|
||||
if len(name) > len(argPrefix) && name[:len(argPrefix)] == argPrefix {
|
||||
paramName := name[4:]
|
||||
return glua.LString(string(api.ctx.QueryArgs().Peek(paramName)))
|
||||
}
|
||||
@ -244,10 +247,10 @@ func (api *ngxVarAPI) getVariable(name string) string {
|
||||
return ""
|
||||
case "server_name":
|
||||
return string(api.ctx.Host())
|
||||
case "arg_":
|
||||
case argPrefix:
|
||||
return string(api.ctx.URI().QueryString())
|
||||
default:
|
||||
if len(name) > 4 && name[:4] == "arg_" {
|
||||
if len(name) > len(argPrefix) && name[:len(argPrefix)] == argPrefix {
|
||||
paramName := name[4:]
|
||||
return string(api.ctx.QueryArgs().Peek(paramName))
|
||||
}
|
||||
|
||||
@ -63,19 +63,17 @@ func (p Phase) String() string {
|
||||
type LuaCoroutine struct {
|
||||
CreatedAt time.Time
|
||||
ExecutionContext context.Context
|
||||
Engine *LuaEngine
|
||||
Co *glua.LState
|
||||
Cancel context.CancelFunc
|
||||
ngxReqAPI *ngxReqAPI
|
||||
RequestCtx *fasthttp.RequestCtx
|
||||
Co *glua.LState
|
||||
ngxVarAPI *ngxVarAPI
|
||||
ngxRespAPI *ngxRespAPI
|
||||
ngxLogAPI *ngxLogAPI
|
||||
Cancel context.CancelFunc
|
||||
executionCancel context.CancelFunc
|
||||
Engine *LuaEngine
|
||||
OutputBuffer []byte
|
||||
Exited bool
|
||||
|
||||
// ngx API 实例(用于测试和 Go 层访问)
|
||||
ngxVarAPI *ngxVarAPI
|
||||
ngxReqAPI *ngxReqAPI
|
||||
ngxRespAPI *ngxRespAPI
|
||||
ngxLogAPI *ngxLogAPI
|
||||
}
|
||||
|
||||
// SetupSandbox 创建 per-request _ENV 沙箱
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user