Skip to content

Commit d028df4

Browse files
committed
Add CORS AllowOriginFunc documentation and recipe
1 parent 06d53e0 commit d028df4

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

cookbook/cors/origin-func/server.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"regexp"
6+
7+
"github.com/labstack/echo/v4"
8+
"github.com/labstack/echo/v4/middleware"
9+
)
10+
11+
var (
12+
users = []string{"Joe", "Veer", "Zion"}
13+
)
14+
15+
func getUsers(c echo.Context) error {
16+
return c.JSON(http.StatusOK, users)
17+
}
18+
19+
// allowOrigin takes the origin as an argument and returns true if the origin
20+
// is allowed or false otherwise.
21+
func allowOrigin(origin string) (bool, error) {
22+
// In this example we use a regular expression but we can imagine various
23+
// kind of custom logic. For example, an external datasource could be used
24+
// to maintain the list of allowed origins.
25+
return regexp.MatchString(`^https:\/\/labstack\.(net|com)$`, origin)
26+
}
27+
28+
func main() {
29+
e := echo.New()
30+
e.Use(middleware.Logger())
31+
e.Use(middleware.Recover())
32+
33+
// CORS restricted with a custom function to allow origins
34+
// and with the GET, PUT, POST or DELETE methods allowed.
35+
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
36+
AllowOriginFunc: allowOrigin,
37+
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
38+
}))
39+
40+
e.GET("/api/users", getUsers)
41+
42+
e.Logger.Fatal(e.Start(":1323"))
43+
}
File renamed without changes.

website/content/cookbook/cors.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ description = "CORS recipe for Echo"
77
parent = "cookbook"
88
+++
99

10-
## Server
10+
## Server using a list of allowed origins
1111

1212
`server.go`
1313

14-
{{< embed "cors/server.go" >}}
14+
{{< embed "cors/origin-list/server.go" >}}
15+
16+
## Server using a custom function to allow origins
17+
18+
`server.go`
19+
20+
{{< embed "cors/origin-func/server.go" >}}
1521

1622
## [Source Code]({{< source "cors" >}})
1723

1824
## Maintainers
1925

2026
- [vishr](https://github.com/vishr)
27+
- [curvegrid](https://github.com/curvegrid)

website/content/middleware/cors.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,41 @@ CORSConfig struct {
3535

3636
// AllowOrigin defines a list of origins that may access the resource.
3737
// Optional. Default value []string{"*"}.
38-
AllowOrigins []string `json:"allow_origins"`
38+
AllowOrigins []string `yaml:"allow_origins"`
39+
40+
// AllowOriginFunc is a custom function to validate the origin. It takes the
41+
// origin as an argument and returns true if allowed or false otherwise. If
42+
// an error is returned, it is returned by the handler. If this option is
43+
// set, AllowOrigins is ignored.
44+
// Optional.
45+
AllowOriginFunc func(origin string) (bool, error) `yaml:"allow_origin_func"`
3946

4047
// AllowMethods defines a list methods allowed when accessing the resource.
4148
// This is used in response to a preflight request.
4249
// Optional. Default value DefaultCORSConfig.AllowMethods.
43-
AllowMethods []string `json:"allow_methods"`
50+
AllowMethods []string `yaml:"allow_methods"`
4451

4552
// AllowHeaders defines a list of request headers that can be used when
46-
// making the actual request. This in response to a preflight request.
53+
// making the actual request. This is in response to a preflight request.
4754
// Optional. Default value []string{}.
48-
AllowHeaders []string `json:"allow_headers"`
55+
AllowHeaders []string `yaml:"allow_headers"`
4956

5057
// AllowCredentials indicates whether or not the response to the request
5158
// can be exposed when the credentials flag is true. When used as part of
5259
// a response to a preflight request, this indicates whether or not the
5360
// actual request can be made using credentials.
5461
// Optional. Default value false.
55-
AllowCredentials bool `json:"allow_credentials"`
62+
AllowCredentials bool `yaml:"allow_credentials"`
5663

5764
// ExposeHeaders defines a whitelist headers that clients are allowed to
5865
// access.
5966
// Optional. Default value []string{}.
60-
ExposeHeaders []string `json:"expose_headers"`
67+
ExposeHeaders []string `yaml:"expose_headers"`
6168

6269
// MaxAge indicates how long (in seconds) the results of a preflight request
6370
// can be cached.
6471
// Optional. Default value 0.
65-
MaxAge int `json:"max_age"`
72+
MaxAge int `yaml:"max_age"`
6673
}
6774
```
6875

0 commit comments

Comments
 (0)