pcors

package module
v0.0.0-...-d2562f1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2016 License: MIT Imports: 2 Imported by: 0

README

Permissive CORS

Godoc

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))
}

Example with reponse header exposing:

package main

import (
	"net/http"

	"github.com/fln/pcors"
)

func main() {
	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",
		pcors.ExposeHeaders("X-First", "X-Second")(http.DefaultServeMux),
	)
}

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

func Default(h http.Handler) http.Handler

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

func ExposeHeaders(headers ...string) func(http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL