cors

package module
v0.0.0-...-58c2acc Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2017 License: MIT Imports: 5 Imported by: 0

README

cors

Build Status Coverage Status

CORS middleware for Go.

Installation

go get -u github.com/go-http-utils/cors

Documentation

API documentation can be found here: https://godoc.org/github.com/go-http-utils/cors

Usage

import (
  "github.com/go-http-utils/cors"
)
mux := http.NewServeMux()
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
  res.Write([]byte("Hello World"))
})

http.ListenAndServe(":8080", cors.Handler(mux))

Documentation

Overview

Example
package main

import (
	"net/http"

	"github.com/go-http-utils/cors"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		res.Write([]byte("Hello World"))
	})

	http.ListenAndServe(":8080", cors.Handler(mux))
}
Output:

Index

Examples

Constants

View Source
const Version = "1.1.0"

Version is this package's version

Variables

This section is empty.

Functions

func Handler

func Handler(h http.Handler, opts ...Option) http.Handler

Handler wraps the http.Handler with CORS support.

Types

type Option

type Option func(*options) error

Option returns a configuration func to configurate the CORS middleware.

func SetAllowHeaders

func SetAllowHeaders(allows []string) Option

SetAllowHeaders configures the Access-Control-Allow-Headers CORS header.

func SetAllowOrigin

func SetAllowOrigin(allow bool) Option

SetAllowOrigin configures the Access-Control-Allow-Origin CORS header. Set to true to reflect the request origin. Set to false to disable CORS.

func SetAllowOriginValidator

func SetAllowOriginValidator(validator func(*http.Request) string) Option

SetAllowOriginValidator configures the Access-Control-Allow-Origin CORS header by run the validator function `func(*http.Request) string`. The validator function accpets an `*http.Request` argument and return the Access-Control-Allow-Origin value.

func SetCredentials

func SetCredentials(credentials bool) Option

SetCredentials configures the Access-Control-Allow-Credentials CORS header.

func SetExposeHeaders

func SetExposeHeaders(exposes []string) Option

SetExposeHeaders configures the Access-Control-Expose-Headers CORS header.

func SetMaxAge

func SetMaxAge(maxAge int) Option

SetMaxAge configures the Access-Control-Max-Age CORS header (in seconds). If the maxAge > 600 (10 minutes), then it will be reset to 600 (10 minutes).

func SetMethods

func SetMethods(methods []string) Option

SetMethods configures the Access-Control-Allow-Methods CORS header.

Jump to

Keyboard shortcuts

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