cors

package module
v0.0.0-...-4fd02a0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2014 License: MIT Imports: 4 Imported by: 0

README

martini-cors-alt

This is a work in progress implementation of CORS middleware for Martini.

It requires a recent Martini with Routes.MethodsFor.

Documentation

Overview

Package cors provides an alternative CORS implementation for Martini.

It relies on the Route.MethodsFor to provide Access-Control-Allow-Methods.

Due to the modularity of Martini, Routes is not available in Middleware handlers so both a Middleware and NotFound handler are required.

Index

Constants

This section is empty.

Variables

View Source
var StandardHeaders = map[string]string{
	"Access-Control-Max-Age":        "86000",
	"Access-Control-Allow-Headers":  "Content-Type, Origin, Authorization",
	"Access-Control-Expose-Headers": "Content-Length",
}

StandardHeaders are an example.

Functions

func RealNotFound

func RealNotFound(w http.ResponseWriter, r *http.Request, routes martini.Routes)

RealNotFound provides an alternative to Martini's inbuilt handler as using a CORS NotFound means we have to handle it ourselves.

Types

type Cors

type Cors struct {
	// Allowed origins is now provided through an interface
	// for more flexibility
	Origins Sites
	// CORS headers. Please use write mutex if updating while live.
	HeadersMutex sync.RWMutex
	// Can use cors.StandardHeaders or provide your own
	Headers map[string]string
	// Default is to return 403 if Origin not a match. Set to true to disable.
	Tolerant bool
	// This is a key to allow overriding Origin with an X-Origin for tools
	// that don't support setting Origin headers eg Postman on a chromebook.
	// Not for production use. Header is called X-Dev
	DevKey string
}

Cors type allows configuration of CORS handling

func (*Cors) MiddleWare

func (cors *Cors) MiddleWare(w http.ResponseWriter, r *http.Request, ctx martini.Context)

Middleware checks the Origin header on requests and adds appropriate CORS headers to the response.

func (*Cors) NotFound

func (cors *Cors) NotFound(w http.ResponseWriter, r *http.Request, routes martini.Routes)

NotFound handles Cors preflight requests. Options routes will shadow it.

type Sites

type Sites interface {
	// SetContext return boolean indicating if origin is known.
	// It may set site in request context if required
	SetContext(string, martini.Context) bool
}

Sites provides the origin check for Cors

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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