Skip to content

Commit 6aa7d9b

Browse files
authored
Merge pull request #45 from reproio/feat/support-regexp
feat: support regexp to transform uri
2 parents 5de8038 + eac1827 commit 6aa7d9b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ path_transforming_rules:
1919
- prefix: /api/v1/hoge
2020
suffix: /start
2121
transformed: /api/v1/hoge/$id/start
22+
- regexp: /api/v1/hoge/[^/]+/stop
23+
transformed: /api/v1/hoge/$id/stop
2224
```
2325
2426
Example `Dockerfile` :

alb_log.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var requestPathRe = regexp.MustCompile(`(?P<method>.*) (?P<protocol>.*)://(?P<ho
7979
type PathTransformingRule struct {
8080
Prefix string
8181
Suffix string
82+
Regexp *regexp.Regexp
8283
Transformed string
8384
}
8485

@@ -96,6 +97,14 @@ func (r *AlbLogRecord) requestPath(rules []PathTransformingRule) (string, error)
9697
}
9798

9899
for _, rule := range rules {
100+
if rule.Regexp != nil {
101+
if rule.Regexp.MatchString(uri) {
102+
match = true
103+
transformed = rule.Transformed
104+
break
105+
}
106+
}
107+
99108
if rule.Prefix != "" && rule.Suffix != "" {
100109
if strings.HasPrefix(uri, rule.Prefix) && strings.HasSuffix(uri, rule.Suffix) {
101110
match = true

alb_log_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"reflect"
5+
"regexp"
56
"testing"
67
"time"
78
)
@@ -257,6 +258,22 @@ func TestAlbLogRecord_RequestPath(t *testing.T) {
257258
want: "/foo/$id/bar",
258259
wantErr: false,
259260
},
261+
{
262+
name: "regexp",
263+
args: args{
264+
paths: []PathTransformingRule{
265+
{
266+
Regexp: regexp.MustCompile(`^/foo/([^/]+)/bar$`),
267+
Transformed: "/foo/$id/bar",
268+
},
269+
},
270+
},
271+
fields: fields{
272+
Request: "POST https://example.com:443/foo/aaa/bar HTTP/1.1",
273+
},
274+
want: "/foo/$id/bar",
275+
wantErr: false,
276+
},
260277
}
261278
for _, tt := range tests {
262279
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)