Documentation
¶
Index ¶
- func CORS(opts ...CORSOption) func(http.Handler) http.Handler
- func CanonicalHost(domain string, code int) func(h http.Handler) http.Handler
- func Compress(level int) func(h http.Handler) http.Handler
- func DefaultCORS(opts ...CORSOption) func(http.Handler) http.Handler
- func DefaultCompress() func(h http.Handler) http.Handler
- func InjectContext(carriers ...contextx.Carrier) httpx.Middleware
- func LogHandler() func(http.Handler) http.Handler
- func MetricHandler(gatherer prometheus.Gatherer) func(http.Handler) http.Handler
- func PProfHandler(enabled bool) func(handler http.Handler) http.Handler
- type CORSOption
- func AllowCredentials() CORSOption
- func AllowedHeaders(headers []string) CORSOption
- func AllowedMethods(methods []string) CORSOption
- func AllowedOriginValidator(fn OriginValidator) CORSOption
- func AllowedOrigins(origins []string) CORSOption
- func ExposedHeaders(headers []string) CORSOption
- func IgnoreOptions() CORSOption
- func MaxAge(age int) CORSOption
- func OptionStatusCode(code int) CORSOption
- type OriginValidator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
func CORS(opts ...CORSOption) func(http.Handler) http.Handler
CORS provides Cross-Origin Resource Sharing middleware. Example:
import (
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/users", UserEndpoint)
r.HandleFunc("/projects", ProjectEndpoint)
// Apply the CORS middleware to our top-level router, with the defaults.
http.ListenAndServe(":8000", handlers.CORS()(r))
}
func CanonicalHost ¶
CanonicalHost is HTTP middleware that re-directs requests to the canonical domain. It accepts a domain and a status code (e.g. 301 or 302) and re-directs clients to this domain. The existing request path is maintained.
Note: If the provided domain is considered invalid by url.Parse or otherwise returns an empty scheme or host, clients are not re-directed.
Example:
r := mux.NewRouter()
canonical := handlers.CanonicalHost("http://www.gorillatoolkit.org", 302)
r.HandleFunc("/route", YourHandler)
log.Fatal(http.ListenAndServe(":7000", canonical(r)))
func DefaultCORS ¶
func DefaultCORS(opts ...CORSOption) func(http.Handler) http.Handler
func InjectContext ¶
func InjectContext(carriers ...contextx.Carrier) httpx.Middleware
func MetricHandler ¶
Types ¶
type CORSOption ¶
type CORSOption func(*corshandler.Cors) error
CORSOption represents a functional option for configuring the CORS middleware.
func AllowCredentials ¶
func AllowCredentials() CORSOption
AllowCredentials can be used to specify that the user agent may pass authentication details along with the request.
func AllowedHeaders ¶
func AllowedHeaders(headers []string) CORSOption
AllowedHeaders adds the provided headers to the list of allowed headers in a CORS request. This is an append operation so the headers Accept, Accept-Language, and Content-Language are always allowed. Content-Type must be explicitly declared if accepting Content-Types other than application/x-www-form-urlencoded, multipart/form-data, or text/plain.
func AllowedMethods ¶
func AllowedMethods(methods []string) CORSOption
AllowedMethods can be used to explicitly allow methods in the Access-Control-Allow-Methods header. This is a replacement operation so you must also pass GET, HEAD, and POST if you wish to support those methods.
func AllowedOriginValidator ¶
func AllowedOriginValidator(fn OriginValidator) CORSOption
AllowedOriginValidator sets a function for evaluating allowed origins in CORS requests, represented by the 'Allow-Access-Control-Origin' HTTP header.
func AllowedOrigins ¶
func AllowedOrigins(origins []string) CORSOption
AllowedOrigins sets the allowed origins for CORS requests, as used in the 'Allow-Access-Control-Origin' HTTP header. Note: Passing in a []string{"*"} will allow any domain.
func ExposedHeaders ¶
func ExposedHeaders(headers []string) CORSOption
ExposedHeaders can be used to specify headers that are available and will not be stripped out by the user-agent.
func IgnoreOptions ¶
func IgnoreOptions() CORSOption
IgnoreOptions causes the CORS middleware to ignore OPTIONS requests, instead passing them through to the next handler. This is useful when your application or framework has a pre-existing mechanism for responding to OPTIONS requests.
func MaxAge ¶
func MaxAge(age int) CORSOption
MaxAge determines the maximum age (in seconds) between preflight requests. A maximum of 10 minutes is allowed. An age above this value will default to 10 minutes.
func OptionStatusCode ¶
func OptionStatusCode(code int) CORSOption
OptionStatusCode sets a custom status code on the OPTIONS requests. Default behavior sets it to 200 to reflect best practices. This is option is not mandatory and can be used if you need a custom status code (i.e. 204).
More information on the spec: https://fetch.spec.whatwg.org/#cors-preflight-fetch
type OriginValidator ¶
type OriginValidator = corshandler.OriginValidator