Skip to content

Commit b59398e

Browse files
authored
feat: patch pkg and cli (#6)
1 parent 7475887 commit b59398e

File tree

12 files changed

+102
-25
lines changed

12 files changed

+102
-25
lines changed

README.md

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

33
# go-ngx-config
44
A nginx config parser
@@ -8,11 +8,15 @@ A nginx config parser
88
### Binary
99
Usage:
1010
```sh
11-
# -f file path location nginx configm, e,g: ./examples/basic/nginx.conf
11+
# Parse
12+
# -f file path location nginx configm, e.g: ./examples/basic/nginx.conf
1213
# -o output json file path location, e.g: ./examples/basic/output
13-
14-
1514
go-ngx-config parse -f <NGINX_CONF_FILE> -o <OUTPUT_JSON_FILE_DUMP>
15+
16+
# Location Matcher
17+
# -f file path location nginx configm, e.g: ./examples/basic/nginx.conf
18+
# -u url target, e.g: /my-location
19+
go-ngx-config lt -f <NGINX_CONF_FILE> -u <URL_TARGET>
1620
```
1721

1822
<details>
@@ -186,7 +190,7 @@ Exported Global Function
186190
// Some Basic Example
187191
await parseConfig(`
188192
server {
189-
server_name my-server.domain.com
193+
server_name my-server.domain.com;
190194
191195
location = /my-location {
192196
proxy_pass http://lite-dev;
@@ -196,21 +200,21 @@ server {
196200

197201
await testLocation(`
198202
server {
199-
server_name my-server.domain.com
203+
server_name my-server.domain.com;
200204
201205
location = /my-location {
202206
proxy_pass http://lite-dev;
203207
}
204208
}
205209
`, '/my-location');
206210
```
207-
![WebExample](./assets/wasm-example.png "Web Example")
208211

209212
<br/>
210213

211214

212215
## TODO(s):
213-
- [ ] Include directive and reads the glob (?)
216+
- [x] .wasm binary
214217
- [x] Location Tester
218+
- [ ] Include directive and reads the glob (?)
215219
- [ ] HTTP Server for see the config on UI browser
216220
- [ ] And lot more...

assets/wasm-example.png

-88.4 KB
Binary file not shown.

cmd/go-ngx-config/commands.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"github.com/adityals/go-ngx-config/pkg/parser"
54
"github.com/spf13/cobra"
65
)
76

87
func NewRootCommand() *cobra.Command {
98
rootCmd := &cobra.Command{
109
Use: "go-ngx-config COMMAND [ARG...]",
11-
Version: parser.VERSION,
1210
Short: "A nginx config build with go",
11+
Version: "0.3.0",
1312
}
1413

1514
return rootCmd

cmd/go-ngx-config/location_tester_command.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ func RunNgxLocationTester(cmd *cobra.Command, args []string) error {
4242
}
4343

4444
elapsed := time.Since(startTime)
45-
logrus.Info("Process time: ", elapsed)
4645

4746
logrus.Info("[Match] Modifier: ", match.MatchModifer)
4847
logrus.Info("[Match] Path: ", match.MatchPath)
4948

49+
logrus.Info("[Match] --- Directives Inside --- ")
50+
for _, d := range match.Directives {
51+
logrus.Info("[Match] Name: ", d.GetName())
52+
for _, param := range d.GetParameters() {
53+
logrus.Info("[Match] Parameters: ", param)
54+
}
55+
}
56+
logrus.Info("[Match] --- End of Directives Inside --- ")
57+
58+
logrus.Info("Process time: ", elapsed)
59+
5060
return nil
5161

5262
}

examples/basic/nginx.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ http {
2727
}
2828

2929
location / {
30+
add_header 'x-foo' 'x-bar';
3031
proxy_pass http://my-upstream;
3132
}
3233

internal/matcher/location.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"strings"
88

99
"github.com/adityals/go-ngx-config/internal/ast"
10+
"github.com/adityals/go-ngx-config/internal/statement"
1011
)
1112

1213
type LocationMatcher struct {
1314
MatchPath string
1415
MatchModifer string
16+
Directives []statement.IDirective
1517
}
1618

1719
const (
@@ -50,7 +52,8 @@ func NewLocationMatcher(conf *ast.Config, targetPath string) (*LocationMatcher,
5052
Name: name,
5153
Modifier: modifier,
5254
Match: match,
53-
Directives: directives})
55+
Directives: directives,
56+
})
5457
}
5558

5659
match, err := locationTester(locations, parsedUrl.Path)
@@ -65,7 +68,9 @@ func NewLocationMatcher(conf *ast.Config, targetPath string) (*LocationMatcher,
6568

6669
return &LocationMatcher{
6770
MatchModifer: match.MatchModifer,
68-
MatchPath: match.MatchPath}, nil
71+
MatchPath: match.MatchPath,
72+
Directives: match.Directives,
73+
}, nil
6974
}
7075

7176
func locationTester(locationsTarget []ast.Location, targetPath string) (*LocationMatcher, error) {
@@ -75,7 +80,11 @@ func locationTester(locationsTarget []ast.Location, targetPath string) (*Locatio
7580
}
7681

7782
if location.Match == targetPath {
78-
return &LocationMatcher{MatchPath: location.Match, MatchModifer: location.Modifier}, nil
83+
return &LocationMatcher{
84+
MatchPath: location.Match,
85+
MatchModifer: location.Modifier,
86+
Directives: location.Directives,
87+
}, nil
7988
}
8089
}
8190

@@ -110,13 +119,21 @@ func locationTester(locationsTarget []ast.Location, targetPath string) (*Locatio
110119

111120
match := reg.FindString(targetPath)
112121
if match != "" {
113-
return &LocationMatcher{MatchPath: location.Match, MatchModifer: location.Modifier}, nil
122+
return &LocationMatcher{
123+
MatchPath: location.Match,
124+
MatchModifer: location.Modifier,
125+
Directives: location.Directives,
126+
}, nil
114127
}
115128
}
116129
}
117130

118131
if bestMatch != nil {
119-
return &LocationMatcher{MatchPath: bestMatch.Match, MatchModifer: bestMatch.Modifier}, nil
132+
return &LocationMatcher{
133+
MatchPath: bestMatch.Match,
134+
MatchModifer: bestMatch.Modifier,
135+
Directives: bestMatch.Directives,
136+
}, nil
120137
}
121138

122139
return nil, nil

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "go-ngx-config",
3+
"description": "Go Nginx Configuration",
4+
"authors": "Aditya Septiadi",
5+
"version": "0.3.0",
6+
"license": "MIT"
7+
}

pkg/matcher/loc_matcher.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package matcher
2+
3+
import (
4+
"errors"
5+
6+
"github.com/adityals/go-ngx-config/internal/matcher"
7+
"github.com/adityals/go-ngx-config/internal/parser"
8+
)
9+
10+
func NewLocationMatcher(locMatcherOpts LocationMatcherOptions) (*matcher.LocationMatcher, error) {
11+
parser, err := parser.NewParser(locMatcherOpts.Filepath)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
parsedConf := parser.Parse()
17+
if parsedConf == nil {
18+
return nil, errors.New("cannot be parsed")
19+
}
20+
21+
match, err := matcher.NewLocationMatcher(parsedConf, locMatcherOpts.UrlTarget)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
if match == nil {
27+
return nil, errors.New("match is nil")
28+
}
29+
30+
return match, nil
31+
}

pkg/matcher/loc_matcher_opts.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package matcher
2+
3+
type LocationMatcherOptions struct {
4+
Filepath string
5+
UrlTarget string
6+
}

pkg/parser/version.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)