Skip to content

Commit 6d389fd

Browse files
authored
Add regex rules for proxy and rewrite middleware (#180)
1 parent 45ac9fc commit 6d389fd

File tree

2 files changed

+95
-33
lines changed

2 files changed

+95
-33
lines changed

website/content/middleware/proxy.md

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description = "Reverse proxy middleware for Echo"
99
Proxy provides an HTTP/WebSocket reverse proxy middleware. It forwards a request
1010
to upstream server using a configured load balancing technique.
1111

12-
_Usage_
12+
### Usage
1313

1414
```go
1515
url1, err := url.Parse("http://localhost:8081")
@@ -32,34 +32,70 @@ e.Use(middleware.Proxy(middleware.NewRoundRobinBalancer([]*middleware.ProxyTarge
3232

3333
## Custom Configuration
3434

35-
_Usage_
35+
### Usage
3636

3737
```go
3838
e := echo.New()
3939
e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{}))
4040
```
4141

42-
## Configuration
42+
### Configuration
4343

4444
```go
4545
// ProxyConfig defines the config for Proxy middleware.
46-
ProxyConfig struct {
47-
// Skipper defines a function to skip middleware.
48-
Skipper Skipper
49-
50-
// Balancer defines a load balancing technique.
51-
// Required.
52-
// Possible values:
53-
// - RandomBalancer
54-
// - RoundRobinBalancer
55-
Balancer ProxyBalancer
56-
}
46+
ProxyConfig struct {
47+
// Skipper defines a function to skip middleware.
48+
Skipper Skipper
49+
50+
// Balancer defines a load balancing technique.
51+
// Required.
52+
Balancer ProxyBalancer
53+
54+
// Rewrite defines URL path rewrite rules. The values captured in asterisk can be
55+
// retrieved by index e.g. $1, $2 and so on.
56+
Rewrite map[string]string
57+
58+
// RegexRewrite defines rewrite rules using regexp.Rexexp with captures
59+
// Every capture group in the values can be retrieved by index e.g. $1, $2 and so on.
60+
RegexRewrite map[*regexp.Regexp]string
61+
62+
// Context key to store selected ProxyTarget into context.
63+
// Optional. Default value "target".
64+
ContextKey string
65+
66+
// To customize the transport to remote.
67+
// Examples: If custom TLS certificates are required.
68+
Transport http.RoundTripper
69+
70+
// ModifyResponse defines function to modify response from ProxyTarget.
71+
ModifyResponse func(*http.Response) error
5772
```
5873
59-
_Default Configuration_
74+
Default Configuration:
6075
61-
| Name | Value |
62-
| ------- | -------------- |
63-
| Skipper | DefaultSkipper |
76+
| Name | Value |
77+
| ---------- | -------------- |
78+
| Skipper | DefaultSkipper |
79+
| ContextKey | `target` |
80+
81+
### Regex-based Rules
82+
83+
For advanced rewriting of proxy requests rules may also be defined using
84+
regular expression. Normal capture groups can be defined using `()` and referenced by index (`$1`, `$2`, ...) for the rewritten path.
85+
86+
`RegexRules` and normal `Rules` can be combined.
87+
88+
```go
89+
e.Use(ProxyWithConfig(ProxyConfig{
90+
Balancer: rrb,
91+
Rewrite: map[string]string{
92+
"^/v1/*": "/v2/$1",
93+
},
94+
RegexRewrite: map[*regexp.Regexp]string{
95+
regexp.MustCompile("^/foo/([0-9].*)"): "/num/$1",
96+
regexp.MustCompile("^/bar/(.+?)/(.*)"): "/baz/$2/$1",
97+
},
98+
}))
99+
```
64100
65101
## [Example](/cookbook/reverse-proxy)

website/content/middleware/rewrite.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ description = "Rewrite middleware for Echo"
66
parent = "middleware"
77
+++
88

9-
Rewrite middleware rewrites the URL path based on provided rules. It can be helpful for backward compatibility or just creating cleaner and more descriptive links.
9+
Rewrite middleware allows to rewrite an URL path based on provided rules. It can be helpful for backward compatibility or just creating cleaner and more descriptive links.
1010

11-
*Usage*
11+
### Usage
1212

1313
```go
1414
e.Pre(middleware.Rewrite(map[string]string{
@@ -20,33 +20,59 @@ e.Pre(middleware.Rewrite(map[string]string{
2020
```
2121

2222
The values captured in asterisk can be retrieved by index e.g. $1, $2 and so on.
23+
Each asterisk will be non-greedy (translated to a capture group `(.*?)`) and if using
24+
multiple asterisk a trailing `*` will match the "rest" of the path.
25+
26+
> Rewrite middleware should be registered via `Echo#Pre()` to get triggered before the router.
2327
2428
## Custom Configuration
2529

26-
*Usage*
30+
### Usage
2731

2832
```go
2933
e := echo.New()
3034
e.Pre(middleware.RewriteWithConfig(middleware.RewriteConfig{}))
3135
```
3236

33-
## Configuration
37+
### Configuration
3438

3539
```go
3640
// RewriteConfig defines the config for Rewrite middleware.
37-
RewriteConfig struct {
38-
// Skipper defines a function to skip middleware.
39-
Skipper Skipper
41+
RewriteConfig struct {
42+
// Skipper defines a function to skip middleware.
43+
Skipper Skipper
44+
45+
// Rules defines the URL path rewrite rules. The values captured in asterisk can be
46+
// retrieved by index e.g. $1, $2 and so on.
47+
Rules map[string]string `yaml:"rules"`
4048

41-
// Rules defines the URL path rewrite rules.
42-
Rules map[string]string `yaml:"rules"`
43-
}
49+
// RegexRules defines the URL path rewrite rules using regexp.Rexexp with captures
50+
// Every capture group in the values can be retrieved by index e.g. $1, $2 and so on.
51+
RegexRules map[*regexp.Regexp]string
52+
}
4453
```
4554

46-
*Default Configuration*
55+
Default Configuration:
4756

48-
Name | Value
49-
---- | -----
50-
Skipper | DefaultSkipper
57+
| Name | Value |
58+
| ------- | -------------- |
59+
| Skipper | DefaultSkipper |
5160

52-
> Rewrite middleware should be registered via `Echo#Pre()` to get triggered before the router.
61+
### Regex-based Rules
62+
63+
For advanced rewriting of paths rules may also be defined using regular expression.
64+
Normal capture groups can be defined using `()` and referenced by index (`$1`, `$2`, ...) for the rewritten path.
65+
66+
`RegexRules` and normal `Rules` can be combined.
67+
68+
```go
69+
e.Pre(RewriteWithConfig(RewriteConfig{
70+
Rewrite: map[string]string{
71+
"^/v1/*": "/v2/$1",
72+
},
73+
RegexRewrite: map[*regexp.Regexp]string{
74+
regexp.MustCompile("^/foo/([0-9].*)"): "/num/$1",
75+
regexp.MustCompile("^/bar/(.+?)/(.*)"): "/baz/$2/$1",
76+
},
77+
}))
78+
```

0 commit comments

Comments
 (0)