-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for configurable monitoring (#11)
- Loading branch information
Showing
21 changed files
with
952 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ on: | |
push: | ||
branches: | ||
- master | ||
- plugin-utils | ||
tags: | ||
- v* | ||
pull_request: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,3 +67,7 @@ issues: | |
- goconst | ||
- funlen | ||
- godot | ||
- lll | ||
- path: config.go | ||
linters: | ||
- lll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,7 @@ testData: | |
rewrites: | ||
- regex: "bar" | ||
replacement: "foo" | ||
monitor: | ||
types: | ||
- text/html | ||
methods: ["GET"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package compressutil | ||
|
||
const ( | ||
// Gzip compression algorithm string. | ||
Gzip string = "gzip" | ||
// Deflate compression algorithm string. | ||
Deflate string = "deflate" | ||
// Identity compression algorithm string. | ||
Identity string = "identity" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package rewrite_body | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/packruler/rewrite-body/httputil" | ||
) | ||
|
||
// Rewrite holds one rewrite body configuration. | ||
type Rewrite struct { | ||
Regex string `json:"regex" yaml:"regex" toml:"regex"` | ||
Replacement string `json:"replacement" yaml:"replacement" toml:"replacement"` | ||
} | ||
|
||
// Config holds the plugin configuration. | ||
type Config struct { | ||
LastModified bool `json:"lastModified" toml:"lastModified" yaml:"lastModified"` | ||
Rewrites []Rewrite `json:"rewrites" toml:"rewrites" yaml:"rewrites"` | ||
LogLevel int8 `json:"logLevel" toml:"logLevel" yaml:"logLevel"` | ||
Monitoring httputil.MonitoringConfig `json:"monitoring" toml:"monitoring" yaml:"monitoring"` | ||
} | ||
|
||
// CreateConfig creates and initializes the plugin configuration. | ||
func CreateConfig() *Config { | ||
return &Config{} | ||
} | ||
|
||
type rewrite struct { | ||
regex *regexp.Regexp | ||
replacement []byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[config] | ||
lastModified = true | ||
logLevel = 0 | ||
[[config.rewrites]] | ||
regex = "test" | ||
replacement = "test" | ||
[config.monitor] | ||
types = ["text/html"] | ||
methods = ["GET"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
config: | ||
lastModified: true | ||
logLevel: 0 | ||
monitor: | ||
types: | ||
- text/html | ||
methods: ["GET"] | ||
rewrites: | ||
- regex: "test" | ||
replacement: "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/packruler/rewrite-body v0.1.4 h1:0JlKMJW7od3DCybLazMBejapOjIdgpwer/BtPqqXDmM= | ||
github.com/packruler/rewrite-body v0.1.4/go.mod h1:QZdekGTvGSTiaWN5PR/dB91IccNbtClM2jpuwqOG4cg= | ||
github.com/packruler/rewrite-body v0.1.5 h1:Z5/uT2I2rJ/9zJWSRMuXbIIt8ETDkv/aCl+OgVObpaI= | ||
github.com/packruler/rewrite-body v0.1.5/go.mod h1:QZdekGTvGSTiaWN5PR/dB91IccNbtClM2jpuwqOG4cg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package httputil | ||
|
||
import "net/http" | ||
|
||
// MonitoringConfig structure of data for handling configuration for | ||
// controlling what content is monitored. | ||
type MonitoringConfig struct { | ||
Types []string `json:"types,omitempty" yaml:"types,omitempty" toml:"types,omitempty" export:"true"` | ||
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty" toml:"methods,omitempty" export:"true"` | ||
} | ||
|
||
// EnsureDefaults check Types and Methods for empty arrays and apply default values if found. | ||
func (config *MonitoringConfig) EnsureDefaults() { | ||
if len(config.Methods) == 0 { | ||
config.Methods = []string{http.MethodGet} | ||
} | ||
|
||
if len(config.Types) == 0 { | ||
config.Types = []string{"text/html"} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package httputil_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/packruler/rewrite-body/httputil" | ||
) | ||
|
||
func TestMonitoringConfigParsing(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
inputConfig httputil.MonitoringConfig | ||
expectedConfig httputil.MonitoringConfig | ||
}{ | ||
{ | ||
desc: "defaults will be supplied for empty arrays", | ||
inputConfig: httputil.MonitoringConfig{ | ||
Types: []string{}, | ||
Methods: []string{}, | ||
}, | ||
expectedConfig: httputil.MonitoringConfig{ | ||
Types: []string{"text/html"}, | ||
Methods: []string{http.MethodGet}, | ||
}, | ||
}, | ||
{ | ||
desc: "defaults will be supplied for empty types with populated methods", | ||
inputConfig: httputil.MonitoringConfig{ | ||
Types: []string{}, | ||
Methods: []string{"POST"}, | ||
}, | ||
expectedConfig: httputil.MonitoringConfig{ | ||
Types: []string{"text/html"}, | ||
Methods: []string{http.MethodPost}, | ||
}, | ||
}, | ||
{ | ||
desc: "defaults will be supplied for populated types with empty methods", | ||
inputConfig: httputil.MonitoringConfig{ | ||
Types: []string{"application/json"}, | ||
Methods: []string{}, | ||
}, | ||
expectedConfig: httputil.MonitoringConfig{ | ||
Types: []string{"application/json"}, | ||
Methods: []string{http.MethodGet}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.desc, func(t *testing.T) { | ||
config := test.inputConfig | ||
|
||
config.EnsureDefaults() | ||
|
||
if len(config.Types) != len(test.expectedConfig.Types) { | ||
t.Errorf("Expected Types: '%v' | Got Types: '%v'", test.expectedConfig.Types, config.Types) | ||
} | ||
|
||
for i, v := range test.expectedConfig.Types { | ||
if v != config.Types[i] { | ||
t.Errorf("Expected Types: '%v' | Got Types: '%v'", test.expectedConfig.Types, config.Types) | ||
} | ||
} | ||
|
||
if len(config.Methods) != len(test.expectedConfig.Methods) { | ||
t.Errorf("Expected Methods: '%v' | Got Methods: '%v'", test.expectedConfig.Methods, config.Methods) | ||
} | ||
|
||
for i, v := range test.expectedConfig.Methods { | ||
if v != config.Methods[i] { | ||
t.Errorf("Expected Methods: '%v' | Got Methods: '%v'", test.expectedConfig.Methods, config.Methods) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.