https://github.com/royge/ezcors
Easy handling of CORS configuration file for rs.cors Options.
https://github.com/royge/ezcors
config configuration cors go golang
Last synced: 3 months ago
JSON representation
Easy handling of CORS configuration file for rs.cors Options.
- Host: GitHub
- URL: https://github.com/royge/ezcors
- Owner: royge
- License: mit
- Created: 2020-05-04T04:45:19.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-12-27T14:42:29.000Z (over 5 years ago)
- Last Synced: 2025-10-17T19:04:06.830Z (8 months ago)
- Topics: config, configuration, cors, go, golang
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ezcors
[](https://travis-ci.org/royge/ezcors)
[](https://goreportcard.com/report/github.com/royge/ezcors)
Easy handling of CORS configuration file for rs.cors Options.
## How To Use
```
go get github.com/royge/ezcors
```
### Example
The `cors.yml` file.
```yaml
dev:
allowedOrigins:
- http://127.0.0.1
- http://devhost.com
allowCredentials: false
allowedMethods:
- GET
- POST
- PUT
- PATCH
- DELETE
debug: true
test:
allowedOrigins:
- http://127.0.0.2
- http://testhost.com
allowCredentials: true
allowedMethods:
- POST
debug: true
stage:
allowedOrigins:
- http://127.0.0.3
- http://stagehost.com
allowCredentials: true
allowedMethods:
- POST
debug: false
prod:
allowedOrigins:
- http://127.0.0.4
- http://prodhost.com
allowCredentials: true
allowedMethods:
- POST
debug: false
```
Usage example:
```go
import (
"fmt"
"github.com/royge/ezcors"
)
// ExampleNewConfig shows an example on how to read configuration from a yaml
// file.
// You can check the content of cors.yml file for reference.
func ExampleNewConfig() {
// Check for CORS config from cors.yml or config/cors.yml.
config, err := ezcors.NewConfig()
if err != nil {
panic("don't panic")
}
fmt.Println("dev allowed origins:", config["dev"].AllowedOrigins)
fmt.Println("dev allowed methods:", config["dev"].AllowedMethods)
fmt.Println("dev allow credentials:", config["dev"].AllowCredentials)
fmt.Println("dev debug:", config["dev"].Debug)
fmt.Println("test allowed origins:", config["test"].AllowedOrigins)
fmt.Println("test allowed methods:", config["test"].AllowedMethods)
fmt.Println("test allow credentials:", config["test"].AllowCredentials)
fmt.Println("test debug:", config["test"].Debug)
fmt.Println("stage allowed origins:", config["stage"].AllowedOrigins)
fmt.Println("prod allowed origins:", config["prod"].AllowedOrigins)
// Check for CORS config from testdata/cors.yml.
config, err = ezcors.NewConfig(ezcors.Option{
Path: "testdata/cors.yml",
})
if err != nil {
panic("don't panic")
}
fmt.Println("-----------")
fmt.Println("Custom Path")
fmt.Println("test allowed origins:", config["test"].AllowedOrigins)
fmt.Println("stage allowed origins:", config["stage"].AllowedOrigins)
fmt.Println("prod allowed origins:", config["prod"].AllowedOrigins)
// Output:
// dev allowed origins: [http://127.0.0.1 http://devhost.com]
// dev allowed methods: [GET POST PUT PATCH DELETE]
// dev allow credentials: false
// dev debug: true
// test allowed origins: [http://127.0.0.2 http://testhost.com]
// test allowed methods: [POST]
// test allow credentials: true
// test debug: true
// stage allowed origins: [http://127.0.0.3 http://stagehost.com]
// prod allowed origins: [http://127.0.0.4 http://prodhost.com]
// -----------
// Custom Path
// test allowed origins: [http://127.0.0.2 http://testhostcustom.com]
// stage allowed origins: [http://127.0.0.3 http://stagehostcustom.com]
// prod allowed origins: [http://127.0.0.4 http://prodhostcustom.com]
}
```