Skip to content

Commit

Permalink
Merge pull request #45 from reproio/feat/support-regexp
Browse files Browse the repository at this point in the history
feat: support regexp to transform uri
  • Loading branch information
takaishi authored Jan 22, 2024
2 parents 5de8038 + eac1827 commit 6aa7d9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ path_transforming_rules:
- prefix: /api/v1/hoge
suffix: /start
transformed: /api/v1/hoge/$id/start
- regexp: /api/v1/hoge/[^/]+/stop
transformed: /api/v1/hoge/$id/stop
```
Example `Dockerfile` :
Expand Down
9 changes: 9 additions & 0 deletions alb_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var requestPathRe = regexp.MustCompile(`(?P<method>.*) (?P<protocol>.*)://(?P<ho
type PathTransformingRule struct {
Prefix string
Suffix string
Regexp *regexp.Regexp
Transformed string
}

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

for _, rule := range rules {
if rule.Regexp != nil {
if rule.Regexp.MatchString(uri) {
match = true
transformed = rule.Transformed
break
}
}

if rule.Prefix != "" && rule.Suffix != "" {
if strings.HasPrefix(uri, rule.Prefix) && strings.HasSuffix(uri, rule.Suffix) {
match = true
Expand Down
17 changes: 17 additions & 0 deletions alb_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"reflect"
"regexp"
"testing"
"time"
)
Expand Down Expand Up @@ -257,6 +258,22 @@ func TestAlbLogRecord_RequestPath(t *testing.T) {
want: "/foo/$id/bar",
wantErr: false,
},
{
name: "regexp",
args: args{
paths: []PathTransformingRule{
{
Regexp: regexp.MustCompile(`^/foo/([^/]+)/bar$`),
Transformed: "/foo/$id/bar",
},
},
},
fields: fields{
Request: "POST https://example.com:443/foo/aaa/bar HTTP/1.1",
},
want: "/foo/$id/bar",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 6aa7d9b

Please sign in to comment.