Skip to content

Commit b57f614

Browse files
committed
Add support for configurable monitoring (traefik#11)
1 parent 11e7460 commit b57f614

21 files changed

+952
-342
lines changed

.devcontainer/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
1717
USER vscode
1818
RUN go install mvdan.cc/gofumpt@latest
1919

20+
ENV YAEGI_VERSION="v0.9.20"
21+
RUN curl -sfL https://raw.githubusercontent.com/traefik/yaegi/master/install.sh | bash -s -- -b $(go env GOPATH)/bin ${YAEGI_VERSION}
22+
2023
# [Optional] Uncomment this line to install global node packages.
2124
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

.devcontainer/devcontainer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@
4444
"remoteUser": "vscode",
4545
"features": {
4646
"git": "latest"
47-
}
47+
},
48+
"workspaceMount": "source=${localWorkspaceFolder},target=/go/src/github.com/${localWorkspaceFolderBasename},type=bind,consistency=cached",
49+
"workspaceFolder": "/go/src/github.com/${localWorkspaceFolderBasename}"
4850
}

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- master
7+
- plugin-utils
78
tags:
89
- v*
910
pull_request:

.golangci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ issues:
6767
- goconst
6868
- funlen
6969
- godot
70+
- lll
71+
- path: config.go
72+
linters:
73+
- lll

.traefik.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ testData:
1111
rewrites:
1212
- regex: "bar"
1313
replacement: "foo"
14+
monitor:
15+
types:
16+
- text/html
17+
methods: ["GET"]

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ experimental:
4343
plugins:
4444
rewrite-body:
4545
moduleName: "github.com/packruler/rewrite-body"
46-
version: "v1.0.1"
46+
version: "v1.1.0"
4747
```
4848
4949
### Dynamic
@@ -77,8 +77,21 @@ http:
7777
replacement: "bar"
7878

7979
# logLevel is optional, defaults to Info level.
80-
# Available logLevels: (Trace: -1, Debug: 1, Info: 2, Warning: 3, Error: 4)
81-
logLevel: 2
80+
# Available logLevels: (Trace: -2, Debug: -1, Info: 0, Warning: 1, Error: 2)
81+
logLevel: 0
82+
83+
# monitoring is optional, defaults to below configuration
84+
# monitoring configuration limits the HTTP queries that are checked for regex replacement.
85+
monitoring:
86+
# methods is a string list. Options are standard HTTP Methods. Entries MUST be ALL CAPS
87+
# For a list of options: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
88+
methods:
89+
- GET
90+
# types is a string list. Options are HTTP Content Types. Entries should match standard formatting
91+
# For a list of options: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
92+
# Wildcards(*) are not supported!
93+
types:
94+
- text/html
8295
services:
8396
my-service:
8497
loadBalancer:

compressutil/compressions.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package compressutil
2+
3+
const (
4+
// Gzip compression algorithm string.
5+
Gzip string = "gzip"
6+
// Deflate compression algorithm string.
7+
Deflate string = "deflate"
8+
// Identity compression algorithm string.
9+
Identity string = "identity"
10+
)

compressutil/compressutil.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ type ReaderError struct {
1717
}
1818

1919
// Decode data in a bytes.Reader based on supplied encoding.
20-
func Decode(byteReader *bytes.Buffer, encoding string) (data []byte, err error) {
20+
func Decode(byteReader *bytes.Buffer, encoding string) ([]byte, error) {
2121
reader, err := getRawReader(byteReader, encoding)
2222
if err != nil {
23-
return nil, &ReaderError{cause: err}
23+
return nil, &ReaderError{
24+
error: err,
25+
cause: err,
26+
}
2427
}
2528

2629
return io.ReadAll(reader)
2730
}
2831

2932
func getRawReader(byteReader *bytes.Buffer, encoding string) (io.Reader, error) {
3033
switch encoding {
31-
case "gzip":
34+
case Gzip:
3235
return gzip.NewReader(byteReader)
3336

34-
case "deflate":
37+
case Deflate:
3538
return flate.NewReader(byteReader), nil
3639

3740
default:
@@ -42,10 +45,10 @@ func getRawReader(byteReader *bytes.Buffer, encoding string) (io.Reader, error)
4245
// Encode data in a []byte based on supplied encoding.
4346
func Encode(data []byte, encoding string) ([]byte, error) {
4447
switch encoding {
45-
case "gzip":
48+
case Gzip:
4649
return compressWithGzip(data)
4750

48-
case "deflate":
51+
case Deflate:
4952
return compressWithZlib(data)
5053

5154
default:

compressutil/compressutil_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ func TestEncode(t *testing.T) {
3333
desc: "should support identity",
3434
input: normalBytes,
3535
expected: normalBytes,
36-
encoding: "identity",
36+
encoding: compressutil.Identity,
3737
shouldMatch: true,
3838
},
3939
{
4040
desc: "should support gzip",
4141
input: normalBytes,
4242
expected: gzippedBytes,
43-
encoding: "gzip",
43+
encoding: compressutil.Gzip,
4444
shouldMatch: false,
4545
},
4646
{
4747
desc: "should support deflate",
4848
input: normalBytes,
4949
expected: deflatedBytes,
50-
encoding: "deflate",
50+
encoding: compressutil.Deflate,
5151
shouldMatch: false,
5252
},
5353
{
@@ -102,21 +102,21 @@ func TestDecode(t *testing.T) {
102102
desc: "should support identity",
103103
input: normalBytes,
104104
expected: normalBytes,
105-
encoding: "identity",
105+
encoding: compressutil.Identity,
106106
shouldMatch: true,
107107
},
108108
{
109109
desc: "should support gzip",
110110
input: gzippedBytes,
111111
expected: normalBytes,
112-
encoding: "gzip",
112+
encoding: compressutil.Gzip,
113113
shouldMatch: false,
114114
},
115115
{
116116
desc: "should support deflate",
117117
input: deflatedBytes,
118118
expected: normalBytes,
119-
encoding: "deflate",
119+
encoding: compressutil.Deflate,
120120
shouldMatch: false,
121121
},
122122
{

config.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package rewrite_body
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/packruler/rewrite-body/httputil"
7+
)
8+
9+
// Rewrite holds one rewrite body configuration.
10+
type Rewrite struct {
11+
Regex string `json:"regex" yaml:"regex" toml:"regex"`
12+
Replacement string `json:"replacement" yaml:"replacement" toml:"replacement"`
13+
}
14+
15+
// Config holds the plugin configuration.
16+
type Config struct {
17+
LastModified bool `json:"lastModified" toml:"lastModified" yaml:"lastModified"`
18+
Rewrites []Rewrite `json:"rewrites" toml:"rewrites" yaml:"rewrites"`
19+
LogLevel int8 `json:"logLevel" toml:"logLevel" yaml:"logLevel"`
20+
Monitoring httputil.MonitoringConfig `json:"monitoring" toml:"monitoring" yaml:"monitoring"`
21+
}
22+
23+
// CreateConfig creates and initializes the plugin configuration.
24+
func CreateConfig() *Config {
25+
return &Config{}
26+
}
27+
28+
type rewrite struct {
29+
regex *regexp.Regexp
30+
replacement []byte
31+
}

0 commit comments

Comments
 (0)