Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/acoshift/cors
CORS middleware for Golang net/http
https://github.com/acoshift/cors
cors golang http middleware
Last synced: about 1 month ago
JSON representation
CORS middleware for Golang net/http
- Host: GitHub
- URL: https://github.com/acoshift/cors
- Owner: acoshift
- License: mit
- Created: 2017-01-20T11:26:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-06T16:27:56.000Z (over 7 years ago)
- Last Synced: 2024-06-20T17:37:40.970Z (7 months ago)
- Topics: cors, golang, http, middleware
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cors
[![Go Report Card](https://goreportcard.com/badge/github.com/acoshift/cors)](https://goreportcard.com/report/github.com/acoshift/cors)
[![GoDoc](https://godoc.org/github.com/acoshift/cors?status.svg)](https://godoc.org/github.com/acoshift/cors)CORS middleware for Golang net/http
### Example
```go
package mainimport (
"fmt"
"net/http""github.com/acoshift/cors"
)func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
}func main() {
h := cors.New(cors.Config{
AllowOrigins: []string{"localhost:8080"},
AllowMethods: []string{http.MethodGet, http.MethodPost},
AllowHeaders: []string{"Authorization"},
AllowCredentials: true,
})(http.HandlerFunc(handler))
http.ListenAndServe(":8080", h)
}
```or use with [middleware](https://github.com/acoshift/middleware)
```go
package mainimport (
"fmt"
"net/http""github.com/acoshift/cors"
"github.com/acoshift/middleware"
)func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
}func main() {
h := middleware.Chain(
cors.New(cors.Config{
AllowOrigins: []string{"localhost:8080"},
AllowMethods: []string{http.MethodGet, http.MethodPost},
AllowHeaders: []string{"Authorization"},
AllowCredentials: true,
}),
)(http.HandlerFunc(handler))
http.ListenAndServe(":8080", h)
}
```