Skip to content

Commit ee09434

Browse files
authored
feat: include directive (#8)
1 parent 2c67299 commit ee09434

File tree

17 files changed

+244
-48
lines changed

17 files changed

+244
-48
lines changed

README.md

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![](https://img.shields.io/badge/version-0.3.0-brightgreen)
1+
![](https://img.shields.io/badge/version-0.4.0-brightgreen)
22

33
# go-ngx-config
44
A nginx config parser
@@ -107,16 +107,73 @@ go-ngx-config lt -f <NGINX_CONF_FILE> -u <URL_TARGET>
107107
]
108108
},
109109
{
110-
"Name": "location",
111-
"Modifier": "=",
112-
"Match": "/favicon.ico",
113-
"Directives": [
110+
"Block": null,
111+
"Name": "include",
112+
"Parameters": [
113+
"conf-includes/proxy.conf"
114+
],
115+
"IncludePath": "conf-includes/proxy.conf",
116+
"Configs": [
114117
{
115-
"Block": null,
116-
"Name": "root",
117-
"Parameters": [
118-
"html"
119-
]
118+
"Directives": [
119+
{
120+
"Block": null,
121+
"Name": "proxy_set_header",
122+
"Parameters": [
123+
"Host",
124+
"$host"
125+
]
126+
}
127+
],
128+
"Filepath": "examples/basic/conf-includes/proxy.conf"
129+
}
130+
]
131+
},
132+
{
133+
"Block": null,
134+
"Name": "include",
135+
"Parameters": [
136+
"handlers/*.conf"
137+
],
138+
"IncludePath": "handlers/*.conf",
139+
"Configs": [
140+
{
141+
"Directives": [
142+
{
143+
"Name": "location",
144+
"Modifier": "=",
145+
"Match": "/my-be-service",
146+
"Directives": [
147+
{
148+
"Block": null,
149+
"Name": "proxy_pass",
150+
"Parameters": [
151+
"http://be-service"
152+
]
153+
}
154+
]
155+
}
156+
],
157+
"Filepath": "examples/basic/handlers/be.conf"
158+
},
159+
{
160+
"Directives": [
161+
{
162+
"Name": "location",
163+
"Modifier": "=",
164+
"Match": "/my-fe-service",
165+
"Directives": [
166+
{
167+
"Block": null,
168+
"Name": "proxy_pass",
169+
"Parameters": [
170+
"http://fe-service"
171+
]
172+
}
173+
]
174+
}
175+
],
176+
"Filepath": "examples/basic/handlers/fe.conf"
120177
}
121178
]
122179
},
@@ -127,9 +184,17 @@ go-ngx-config lt -f <NGINX_CONF_FILE> -u <URL_TARGET>
127184
"Directives": [
128185
{
129186
"Block": null,
130-
"Name": "root",
187+
"Name": "add_header",
131188
"Parameters": [
132-
"html"
189+
"'x-foo'",
190+
"'x-bar'"
191+
]
192+
},
193+
{
194+
"Block": null,
195+
"Name": "proxy_pass",
196+
"Parameters": [
197+
"http://my-upstream"
133198
]
134199
}
135200
]
@@ -152,7 +217,9 @@ go-ngx-config lt -f <NGINX_CONF_FILE> -u <URL_TARGET>
152217
"Name": "include",
153218
"Parameters": [
154219
"mime.types"
155-
]
220+
],
221+
"IncludePath": "mime.types",
222+
"Configs": null
156223
},
157224
{
158225
"Block": null,
@@ -215,6 +282,6 @@ server {
215282
## TODO(s):
216283
- [x] .wasm binary
217284
- [x] Location Tester
218-
- [ ] Include directive and reads the glob (?)
285+
- [x] Include directive and reads the glob
219286
- [ ] HTTP Server for see the config on UI browser
220287
- [ ] And lot more...

cmd/go-ngx-config-wasm/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func registerCallbacks() {
2828
}
2929

3030
func parseConfig(confString string) (*ast.Config, error) {
31-
parser := parser.NewStringParser(confString)
31+
// ! we mark parseInclude as false because web still getting from text input
32+
parser := parser.NewStringParser(confString, false)
3233

3334
ast := parser.Parse()
3435
if ast == nil {

cmd/go-ngx-config/commands.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func NewRootCommand() *cobra.Command {
88
rootCmd := &cobra.Command{
99
Use: "go-ngx-config COMMAND [ARG...]",
1010
Short: "A nginx config build with go",
11-
Version: "0.3.0",
11+
Version: "0.4.0",
1212
}
1313

1414
return rootCmd
@@ -22,6 +22,7 @@ func NewParseCommand() *cobra.Command {
2222
}
2323

2424
parseCmd.Flags().StringP("file", "f", "", "nginx.conf file location")
25+
parseCmd.Flags().BoolP("include", "i", false, "parse include")
2526
parseCmd.Flags().StringP("output", "o", "", "output file location")
2627

2728
return parseCmd
@@ -35,6 +36,7 @@ func NewLocationTesterCommand() *cobra.Command {
3536
}
3637

3738
testCmd.Flags().StringP("file", "f", "", "nginx.conf file location")
39+
testCmd.Flags().BoolP("include", "i", false, "parse include")
3840
testCmd.Flags().StringP("url", "u", "", "target url")
3941

4042
return testCmd

cmd/go-ngx-config/location_tester_command.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,28 @@ import (
1313
func RunNgxLocationTester(cmd *cobra.Command, args []string) error {
1414
startTime := time.Now()
1515

16+
logrus.Info("Test location match")
17+
1618
filePath, err := cmd.Flags().GetString("file")
1719
if err != nil {
1820
return err
1921
}
2022

23+
parseInclude, err := cmd.Flags().GetBool("include")
24+
if err != nil {
25+
return err
26+
}
27+
2128
targetUrl, err := cmd.Flags().GetString("url")
2229
if err != nil {
2330
return err
2431
}
2532

33+
logrus.Info("Parsing include: ", parseInclude)
34+
2635
parserOpts := parser.NgxConfParserCliOptions{
27-
Filepath: filePath,
36+
Filepath: filePath,
37+
ParseInclude: parseInclude,
2838
}
2939

3040
ast, err := parser.NewNgxConfParser(parserOpts)

cmd/go-ngx-config/parser_command.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,31 @@ import (
1212
)
1313

1414
func RunParseNgx(cmd *cobra.Command, args []string) error {
15+
1516
startTime := time.Now()
1617

18+
logrus.Info("Parsing nginx config")
19+
1720
filePath, err := cmd.Flags().GetString("file")
1821
if err != nil {
1922
return err
2023
}
2124

25+
parseInclude, err := cmd.Flags().GetBool("include")
26+
if err != nil {
27+
return err
28+
}
29+
2230
outputFilePath, err := cmd.Flags().GetString("output")
2331
if err != nil {
2432
return err
2533
}
2634

35+
logrus.Info("Parsing include: ", parseInclude)
36+
2737
parserOpts := parser.NgxConfParserCliOptions{
28-
Filepath: filePath,
38+
Filepath: filePath,
39+
ParseInclude: parseInclude,
2940
}
3041

3142
ast, err := parser.NewNgxConfParser(parserOpts)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
proxy_set_header Host $host;

examples/basic/handlers/be.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
location = /my-be-service {
2+
proxy_pass http://be-service;
3+
}

examples/basic/handlers/fe.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
location = /my-fe-service {
2+
proxy_pass http://fe-service;
3+
}

examples/basic/nginx.conf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ http {
2222

2323
error_page 500 502 503 504 /50x.html;
2424

25-
location = /favicon.ico {
26-
root html;
27-
}
25+
include conf-includes/proxy.conf;
26+
include handlers/*.conf;
2827

2928
location / {
3029
add_header 'x-foo' 'x-bar';

internal/ast/include.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ast
22

33
import (
4+
"errors"
5+
46
"github.com/adityals/go-ngx-config/internal/directive"
57
"github.com/adityals/go-ngx-config/internal/statement"
68
)
@@ -11,6 +13,21 @@ type Include struct {
1113
Configs []*Config
1214
}
1315

16+
func NewInclude(d *directive.Directive) (*Include, error) {
17+
if d.Block != nil {
18+
return nil, errors.New("include cannot have a block")
19+
}
20+
21+
if len(d.Parameters) != 1 {
22+
return nil, errors.New("include must have 1 parameter")
23+
}
24+
25+
return &Include{
26+
Directive: d,
27+
IncludePath: d.Parameters[0],
28+
}, nil
29+
}
30+
1431
func (c *Include) GetDirectives() []statement.IDirective {
1532
directives := make([]statement.IDirective, 0)
1633
for _, config := range c.Configs {

0 commit comments

Comments
 (0)