https://github.com/heppu/simple-cors
Dead simple CORS handler for Go
https://github.com/heppu/simple-cors
cors go web-api
Last synced: 10 months ago
JSON representation
Dead simple CORS handler for Go
- Host: GitHub
- URL: https://github.com/heppu/simple-cors
- Owner: heppu
- License: mit
- Created: 2016-02-22T18:54:57.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-03-13T07:41:02.000Z (over 9 years ago)
- Last Synced: 2025-04-21T02:40:50.910Z (about 1 year ago)
- Topics: cors, go, web-api
- Language: Go
- Size: 6.84 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dead Simple CORS
CORS handling made as easy as it gets.
You can use this package when you want to allow all cross origin requests. It works with every http.Handler compatible router. Since the package is just one function I suggest that you just copy the code instead of importing it.
## Example
```go
package main
import (
"io"
"net/http"
"github.com/heppu/simple-cors"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
http.ListenAndServe(":8000", cors.CORS(mux))
}
```
## How?
By setting following headers:
```
"Access-Control-Allow-Origin" : origin of request or * as a fallback
"Access-Control-Allow-Methods" : "POST, GET, OPTIONS, PUT, DELETE, HEAD, PATCH"
"Access-Control-Allow-Headers" : "Accept, Accept-Encoding, Authorization, Content-Length, Content-Type, X-CSRF-Token"
"Access-Control-Expose-Headers" : same as above
"Access-Control-Allow-Credentials" : true
```
## Licence
MIT licensed. See the LICENSE file for details.