github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/http/cors/cors.go (about)

     1  // Package cors provides CORS support.
     2  package cors
     3  
     4  import (
     5  	"net/http"
     6  
     7  	"github.com/rs/cors"
     8  
     9  	"github.com/apex/up"
    10  	"github.com/apex/up/config"
    11  )
    12  
    13  // New CORS handler.
    14  func New(c *up.Config, next http.Handler) http.Handler {
    15  	if c.CORS == nil {
    16  		return next
    17  	}
    18  
    19  	return cors.New(options(c.CORS)).Handler(next)
    20  }
    21  
    22  // options returns the canonical options.
    23  func options(c *config.CORS) cors.Options {
    24  	return cors.Options{
    25  		AllowedOrigins:   c.AllowedOrigins,
    26  		AllowedMethods:   c.AllowedMethods,
    27  		AllowedHeaders:   c.AllowedHeaders,
    28  		ExposedHeaders:   c.ExposedHeaders,
    29  		AllowCredentials: c.AllowCredentials,
    30  		MaxAge:           c.MaxAge,
    31  		Debug:            c.Debug,
    32  	}
    33  }