Documentation
¶
Overview ¶
Package pcors provides implementation of fully permissive CORS middleware.
This package is intended for web services that does not require a strict cross-origin resource sharing rules and would like to expose API endpoints to all origins, allow all methods, all request headers including credentials with minimal configuration. CORS pre-fligh request also hints client to cache the response for up to 24 hours.
Example usage:
package main
import (
"net/http"
"github.com/fln/pcors"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
})
http.ListenAndServe(":8123", pcors.Default(http.DefaultServeMux))
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Default ¶
Default is a standard permissive CORS middleware. It will allow all cross-origin requests. However this middleware will not expose any of the response headers.
Example ¶
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
})
http.ListenAndServe(":8123", Default(http.DefaultServeMux))
func ExposeHeaders ¶
ExposeHeaders generates permissive CORS middleware similar to Default one, but also exposes given list of response headers.
Example ¶
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-First", "first exposed")
w.Header().Set("X-Second", "second exposed")
w.Header().Set("X-Third", "not exposed")
w.Write([]byte("Hello world!"))
})
http.ListenAndServe(
":8123",
ExposeHeaders("X-First", "X-Second")(http.DefaultServeMux),
)
Types ¶
This section is empty.