ClessLi / bifrost

Web服务器配置文件(Nginx config等)解析工具,提供配置文件展示和修改接口
MIT License
37 stars 11 forks source link

nginx parse cannot parse lua script #9

Open lwfcatch opened 2 years ago

lwfcatch commented 2 years ago
server {
    server_name test.xxxx.com;
    listen 80;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $http_x_forwarded_protocol;
    proxy_set_header X-Rewrite-URL $request_uri;

    location /test/ {

        set_by_lua_block $foo {
            local ok, err = pcall(require("phases_modules").set_process)
            if not ok then
            local emsg = "[LUA Framework], set_process failed."
            ngx.log(ngx.ERR, emsg, err)
            end
        }

        if ($request_uri !~* ^/test-resource/goofy/(.*)$ ) {
            rewrite ^/test-resource/(.*)$ /test-resource/goofy/$1 break;
        }

        proxy_pass http://test2.xxx.com;
    }
}
func main() {
    nginxConfFromPath, err := configuration.NewConfigurationFromPath("/Users/catch/Desktop/test.nginx")
    if err != nil {
        panic(err)
    }
    fmt.Println(string(nginxConfFromPath.Json()))
}

panic: runtime error: index out of range [-1]

goroutine 1 [running]: github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/loader.(loader).loadFromConfigPosition(0xc000023040, 0x41a1fab, 0x23, 0x0, 0x0, 0x0, 0x0) /Users/catch/go/src/github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/loader/loader.go:249 +0xd45 github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/loader.(loader).LoadFromFilePath(0xc000023040, 0x41a1fab, 0x23, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0) /Users/catch/go/src/github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/loader/loader.go:43 +0x32f github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/configuration.NewConfigurationFromPath(0x41a1fab, 0x23, 0xc000117f78, 0x400b465, 0xc000070058, 0x0) /Users/catch/go/src/github.com/ClessLi/bifrost/pkg/resolv/V2/nginx/configuration/configuration.go:292 +0x92 main.main() /Users/catch/Documents/code/deliver/go/plugin.go:119 +0x3a

ClessLi commented 2 years ago

The nginx.Parser does not support fields or contexts in the format of Lua Scripts currently. You can consider first to separate Lua Scripts into script files and set a parser.Key for it. The sample configuration is as follows:

...
location /test/ {

        set_by_lua_block_file ../block.lua;
...

At present, I am reconstructing the Heimdallr-reborn project. After completion, I will consider allowing Bifrost to support custom format fields or contexts such as Lua Scripts; Or if you have a better solution, you are welcome to submit a PR.