enhance(gonginx): lua code parse and format dump

Close #307
This commit is contained in:
Jacky 2025-04-14 18:08:55 +08:00
parent 92dc61ecb4
commit 1fe6b8b031
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
5 changed files with 35 additions and 6 deletions

5
go.mod
View file

@ -149,6 +149,7 @@ require (
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.143 // indirect
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df // indirect
github.com/imega/luaformatter v0.0.0-20211025140405-86b0a68d6bef // indirect
github.com/infobloxopen/infoblox-go-client v1.1.1 // indirect
github.com/itchyny/timefmt-go v0.1.6 // indirect
github.com/jackc/pgio v1.0.0 // indirect
@ -232,6 +233,8 @@ require (
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1140 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1136 // indirect
github.com/timtadh/data-structures v0.5.3 // indirect
github.com/timtadh/lexmachine v0.2.2 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
@ -291,3 +294,5 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
replace github.com/tufanbarisyildirim/gonginx => github.com/0xJacky/gonginx v0.0.0-20250415011133-af8a79b2217e

11
go.sum
View file

@ -606,6 +606,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
github.com/0xJacky/gonginx v0.0.0-20250415011133-af8a79b2217e h1:JkcKe42N9mziSykLYCfLbvStpmSuYhTtIW/sbkfRiN8=
github.com/0xJacky/gonginx v0.0.0-20250415011133-af8a79b2217e/go.mod h1:2TL7nwvPaCiVV1sFV0MXqHlIy0aGrcv/CIjaUp1hqbE=
github.com/0xJacky/pofile v1.0.0 h1:ZjfpvLlouhnzOsSGhJ/dmqp5DkKg7XGjuulAAXVnhkE=
github.com/0xJacky/pofile v1.0.0/go.mod h1:qq7YtcX4V35EBfOypsYLuLO7hCBExAH9q7xOxTqv2lQ=
github.com/AdamSLevy/jsonrpc2/v14 v14.1.0 h1:Dy3M9aegiI7d7PF1LUdjbVigJReo+QOceYsMyFh9qoE=
@ -1229,6 +1231,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df h1:MZf03xP9WdakyXhOWuAD5uPK3wHh96wCsqe3hCMKh8E=
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
github.com/imega/luaformatter v0.0.0-20211025140405-86b0a68d6bef h1:RC993DdTIHNItsyLj79fgZNLzrf9tBN0GR6W5ZPms6s=
github.com/imega/luaformatter v0.0.0-20211025140405-86b0a68d6bef/go.mod h1:i2XCfvmO94HrEOQWllihhtPrkvNfuB2R2p/o6+OVnRU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
@ -1763,6 +1767,10 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1140 h1:F/v
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1140/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1136 h1:kMIdSU5IvpOROh27ToVQ3hlm6ym3lCRs9tnGCOBoZqk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1136/go.mod h1:FpyIz3mymKaExVs6Fz27kxDBS42jqZn7vbACtxdeEH4=
github.com/timtadh/data-structures v0.5.3 h1:F2tEjoG9qWIyUjbvXVgJqEOGJPMIiYn7U5W5mE+i/vQ=
github.com/timtadh/data-structures v0.5.3/go.mod h1:9R4XODhJ8JdWFEI8P/HJKqxuJctfBQw6fDibMQny2oU=
github.com/timtadh/lexmachine v0.2.2 h1:g55RnjdYazm5wnKv59pwFcBJHOyvTPfDEoz21s4PHmY=
github.com/timtadh/lexmachine v0.2.2/go.mod h1:GBJvD5OAfRn/gnp92zb9KTgHLB7akKyxmVivoYCcjQI=
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
@ -1772,8 +1780,6 @@ github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XV
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/transip/gotransip/v6 v6.26.0 h1:Aejfvh8rSp8Mj2GX/RpdBjMCv+Iy/DmgfNgczPDP550=
github.com/transip/gotransip/v6 v6.26.0/go.mod h1:x0/RWGRK/zob817O3tfO2xhFoP1vu8YOHORx6Jpk80s=
github.com/tufanbarisyildirim/gonginx v0.0.0-20250225174229-c03497ddaef6 h1:HmtcQ7w07RI2SdTKkPf+NM8R33B1oR9MjIZYzlBizwA=
github.com/tufanbarisyildirim/gonginx v0.0.0-20250225174229-c03497ddaef6/go.mod h1:hdMWBc1+TyB6G5ZZBBgPWQ8cjRZ6zpYdhal0uu6E9QM=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
@ -2709,6 +2715,7 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View file

@ -3,7 +3,6 @@ package nginx
import (
"github.com/tufanbarisyildirim/gonginx/dumper"
"github.com/tufanbarisyildirim/gonginx/parser"
"github.com/uozi-tech/cosy/logger"
)
func (c *NgxConfig) FmtCode() (fmtContent string) {
@ -12,7 +11,6 @@ func (c *NgxConfig) FmtCode() (fmtContent string) {
}
func FmtCode(content string) (fmtContent string, err error) {
logger.Debugf("content: %s", content)
p := parser.NewStringParser(content, parser.WithSkipValidDirectivesErr())
c, err := p.Parse()
if err != nil {

View file

@ -5,6 +5,7 @@ import (
"github.com/pkg/errors"
"github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/dumper"
"github.com/tufanbarisyildirim/gonginx/parser"
)
@ -52,6 +53,13 @@ func (l *NgxLocation) parseLocation(directive config.IDirective, deep int) {
if directive.GetBlock() == nil {
return
}
if directive.GetBlock().GetCodeBlock() != "" {
// deep copy
style := *dumper.IndentedStyle
style.StartIndent = deep * style.Indent
l.Content += dumper.DumpLuaBlock(directive.GetBlock(), &style) + "\n"
return
}
for _, location := range directive.GetBlock().GetDirectives() {
if len(location.GetComment()) > 0 {
for _, c := range location.GetComment() {
@ -66,7 +74,7 @@ func (l *NgxLocation) parseLocation(directive config.IDirective, deep int) {
if location.GetBlock() != nil && location.GetBlock().GetDirectives() != nil {
l.Content += " { \n"
l.parseLocation(location, deep+1)
l.Content += " } \n"
l.Content += strings.Repeat("\t", deep) + "} \n"
} else {
l.Content += ";\n"
}
@ -146,8 +154,14 @@ func (c *NgxConfig) parseCustom(directive config.IDirective) {
for _, param := range v.GetParameters() {
params = append(params, param.Value)
}
inlineComment := ""
for _, inline := range v.GetInlineComment() {
inlineComment += inline.Value + " "
}
c.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
v.GetName() + " " + strings.Join(params, " ") + ";\n"
v.GetName() + " " + strings.Join(params, " ") + ";" + inlineComment + "\n"
}
c.Custom += "}\n"
}
@ -190,6 +204,10 @@ func parse(block config.IBlock, ngxConfig *NgxConfig) (err error) {
ngxConfig.parseCustom(v)
}
}
if strings.TrimSpace(ngxConfig.Custom) == "" {
return
}
custom, err := FmtCode(ngxConfig.Custom)
if err != nil {
return

View file

@ -102,5 +102,6 @@ func main() {
Program: Program(confPath),
Address: fmt.Sprintf("%s:%d", cSettings.ServerSettings.Host, cSettings.ServerSettings.Port),
TerminateTimeout: 5 * time.Second,
Debug: cSettings.ServerSettings.RunMode == gin.DebugMode,
})
}