cors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2017 License: MIT Imports: 6 Imported by: 0

README

cors

Go Report Card GoDoc

CORS middleware for Golang net/http

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/acoshift/cors"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "OK")
}

func main() {
	h := cors.New(cors.Config{
		AllowOrigins:     []string{"localhost:8080"},
		AllowMethods:     []string{http.MethodGet, http.MethodPost},
		AllowHeaders:     []string{"Authorization"},
		AllowCredentials: true,
	})(http.HandlerFunc(handler))
	http.ListenAndServe(":8080", h)
}

or use with middleware

package main

import (
	"fmt"
	"net/http"

	"github.com/acoshift/cors"
	"github.com/acoshift/middleware"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "OK")
}

func main() {
	h := middleware.Chain(
		cors.New(cors.Config{
			AllowOrigins:     []string{"localhost:8080"},
			AllowMethods:     []string{http.MethodGet, http.MethodPost},
			AllowHeaders:     []string{"Authorization"},
			AllowCredentials: true,
		}),
	)(http.HandlerFunc(handler))
	http.ListenAndServe(":8080", h)
}

Documentation

Overview

Package cors provides native http middleware for cors see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config Config) middleware.Middleware

New creates new CORS middleware

Types

type Config

type Config struct {
	Skipper          middleware.Skipper
	AllowAllOrigins  bool
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	AllowCredentials bool
	ExposeHeaders    []string
	MaxAge           time.Duration
}

Config is the cors config

Jump to

Keyboard shortcuts

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