Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nginx conf #1130

Merged
merged 23 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get server info from nginx.conf
  • Loading branch information
suchen-sci committed Oct 31, 2023
commit c8fa3e27806c9faf74c4e7eb59c6536a4e7e593c
2 changes: 1 addition & 1 deletion cmd/client/commandv2/convert/nginx/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func Cmd() *cobra.Command {
if err != nil {
general.ExitWithErrorf("parse nginx.conf failed: %v", err)
}
parsePayload(payload)
data, err := json.Marshal(payload)
if err != nil {
general.ExitWithError(err)
}
fmt.Println(string(data))
general.Warnf("warn")
},
}
cmd.Flags().StringVarP(&nginxConf, "file", "f", "", "nginx.conf file path")
Expand Down
44 changes: 0 additions & 44 deletions cmd/client/commandv2/convert/nginx/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,3 @@
*/

package nginx

import (
"github.com/megaease/easegress/v2/cmd/client/general"
crossplane "github.com/nginxinc/nginx-go-crossplane"
)

const (
DirectiveInclude = "include"
DirectiveHTTP = "http"
DirectiveServer = "server"
DirectiveLocation = "location"
)

// loadIncludes loads include files recursively.
// The result contains all directives in the include files and the original directives.
func loadIncludes(fileName string, directives crossplane.Directives, payload *crossplane.Payload) crossplane.Directives {
res := crossplane.Directives{}
for _, d := range directives {
if d.Directive != DirectiveInclude {
d.File = fileName
res = append(res, d)
continue
}

if len(d.Args) != 1 {
continue
}
name := d.Args[0]
var include crossplane.Directives
for _, config := range payload.Config {
if config.File == name {
include = config.Parsed
break
}
}
if include == nil {
general.Warnf("can't find include file %s in line %d of file %s", name, d.Line, fileName)
continue
}
include = loadIncludes(name, include, payload)
res = append(res, include...)
}
return res
}
93 changes: 0 additions & 93 deletions cmd/client/commandv2/convert/nginx/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,96 +16,3 @@
*/

package nginx

import (
"fmt"
"testing"

crossplane "github.com/nginxinc/nginx-go-crossplane"
"github.com/stretchr/testify/assert"
)

func TestLoadIncludes(t *testing.T) {
tempDir := newTempTestDir(t)
defer tempDir.Clean()

{
conf := `
events {}
http {
include %s;
server {
listen 80;
location / {
proxy_pass https://localhost:8888;
}
}
}
`
conf1 := `include %s; proxy_set_header Conf-One One;`
conf2 := `proxy_set_header Conf-Two Two;`

file2 := tempDir.Create(t, []byte(conf2))
file1 := tempDir.Create(t, []byte(fmt.Sprintf(conf1, file2)))
file := tempDir.Create(t, []byte(fmt.Sprintf(conf, file1)))

payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)
httpDirectives := payload.Config[0].Parsed[1].Block
httpDirectives = loadIncludes(file, httpDirectives, payload)
assert.Equal(t, 3, len(httpDirectives))

// first directive from conf2
d2 := httpDirectives[0]
assert.Equal(t, "proxy_set_header", d2.Directive)
assert.Equal(t, []string{"Conf-Two", "Two"}, d2.Args)
assert.Equal(t, file2, d2.File)
// second directive from conf1
d1 := httpDirectives[1]
assert.Equal(t, "proxy_set_header", d1.Directive)
assert.Equal(t, []string{"Conf-One", "One"}, d1.Args)
assert.Equal(t, file1, d1.File)
}

{
// test invalid includes
conf := `
events {}
http {
include not-exist.conf;
include %s invalid-args.conf;
include;
include %s;
server {
listen 80;
location / {
proxy_pass https://localhost:8888;
}
}
}
`
conf1 := `include %s; proxy_set_header Conf-One One;`
conf2 := `proxy_set_header Conf-Two Two;`

file2 := tempDir.Create(t, []byte(conf2))
file1 := tempDir.Create(t, []byte(fmt.Sprintf(conf1, file2)))
file := tempDir.Create(t, []byte(fmt.Sprintf(conf, file1, file1)))

payload, err := crossplane.Parse(file, &crossplane.ParseOptions{})
assert.Nil(t, err)
httpDirectives := payload.Config[0].Parsed[1].Block
httpDirectives = loadIncludes(file, httpDirectives, payload)
assert.Equal(t, 3, len(httpDirectives))

// first directive from conf2
d2 := httpDirectives[0]
assert.Equal(t, "proxy_set_header", d2.Directive)
assert.Equal(t, []string{"Conf-Two", "Two"}, d2.Args)
assert.Equal(t, file2, d2.File)
// second directive from conf1
d1 := httpDirectives[1]
assert.Equal(t, "proxy_set_header", d1.Directive)
assert.Equal(t, []string{"Conf-One", "One"}, d1.Args)
assert.Equal(t, file1, d1.File)
}
}
Loading