Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mununki/go-cors

A module to help to set up CORS for Go API or web Server
https://github.com/mununki/go-cors

cors go golang

Last synced: 10 days ago
JSON representation

A module to help to set up CORS for Go API or web Server

Awesome Lists containing this project

README

        

# Go-CORS

This is a module to handle CORS for Go web API or Web Server.

## Usage

```go
mux.Handle("/", goCors.Set(http.Handler))
```

> NOTE : Be aware of setting CORS `*` for `dev` mode only, in case of `prod`, BE careful it will allow any requests from any origin.

## Example

```go
import (
"log"
"net/http"

goCors "github.com/mattdamon108/go-cors"
)

func main() {
mux := http.NewServeMux()
mux.Handle("/", goCors.Set(Index{}))

s := &http.Server{
Addr: ":8080",
Handler: mux,
}

log.Println("Listening to... port 8080")
if err := s.ListenAndServe(); err != nil {
panic(err)
}
}

type Index struct{}

func (i Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := "This is CORS test on path: " + r.URL.Path
w.Write([]byte(path))
}
```

# Next to do

- [ ] Add the whitelist