Skip to content

Commit

Permalink
add uri_filter caddyfile directive
Browse files Browse the repository at this point in the history
  • Loading branch information
greenpau committed Feb 14, 2021
1 parent 1e96f14 commit 4bd1bcc
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 136 deletions.
117 changes: 4 additions & 113 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ multiple handlers and there is a way to deferentiate between them.
The `response_debug` argument instructs the plugin to buffer
responses and log response related metadata, i.e. status codes, length, etc.

The `uri_filter` directive instructs the plugin to intercepts only
the requests with the URI matching the regular expression in the filter.

When a request arrives for `/version`, the plugin will be triggered two (2)
times. The first handler is disables. The two (2) other handlers will trigger
with different tags. The `respond` handler is terminal and it means the handler
Expand All @@ -64,122 +67,10 @@ localhost:9080 {
trace disabled=no tag="bar"
respond /version "1.0.0" 200
trace tag="marvel" response_debug=yes
trace tag="custom" response_debug=yes uri_filter="^/whoami$"
respond /whoami 200 {
body "greenpau"
}
}
}
```

The same JSON configuration:

```json
{
"apps": {
"http": {
"http_port": 9080,
"https_port": 9443,
"servers": {
"srv0": {
"listen": [
":9080"
],
"routes": [
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"disabled": true,
"handler": "trace"
}
]
},
{
"handle": [
{
"handler": "trace",
"tag": "foo"
}
]
},
{
"handle": [
{
"handler": "trace",
"tag": "bar"
}
]
},
{
"handle": [
{
"body": "1.0.0",
"handler": "static_response",
"status_code": 200
}
],
"match": [
{
"path": [
"/version"
]
}
]
},
{
"handle": [
{
"handler": "trace",
"response_debug_enabled": true,
"tag": "marvel"
}
]
},
{
"handle": [
{
"body": "greenpau",
"handler": "static_response",
"status_code": 200
}
],
"match": [
{
"path": [
"/whoami*"
]
}
]
}
]
}
]
}
]
}
],
"match": [
{
"host": [
"localhost"
]
}
],
"terminal": true
}
]
}
}
}
}
}
```
6 changes: 6 additions & 0 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"regexp"
"strings"
)

Expand Down Expand Up @@ -64,6 +65,11 @@ func parseCaddyfileRequestDebugger(h httpcaddyfile.Helper) (caddyhttp.Middleware
if isEnabledArg(v) {
dbg.ResponseDebugEnabled = true
}
case "uri_filter":
dbg.URIFilter = v
if _, err := regexp.CompilePOSIX(dbg.URIFilter); err != nil {
return nil, fmt.Errorf("%s directive value of %s fails to compile: %s", k, v, err)
}
default:
return nil, fmt.Errorf("unsupported argument: %s", arg)
}
Expand Down
1 change: 1 addition & 0 deletions caddyfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestCaddyfile(t *testing.T) {
trace disabled=no tag="bar"
respond /version "1.0.0" 200
trace tag="marvel" response_debug=yes
trace tag="custom" response_debug=yes uri_filter="^/whoami$"
respond /whoami* 200 {
body "greenpau"
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/greenpau/caddy-trace
go 1.14

require (
github.com/caddyserver/caddy/v2 v2.2.0
github.com/caddyserver/caddy/v2 v2.3.0
github.com/satori/go.uuid v1.2.0
go.uber.org/zap v1.16.0
)
Loading

0 comments on commit 4bd1bcc

Please sign in to comment.