talira

Configurable CORS handler for Go's net/http

open sourceMITfree

rs/cors is a Go net/http handler that implements the W3C Cross-Origin Resource Sharing specification. It sets the Access-Control headers for allowed origins, methods and headers, answers preflight requests, and takes options for origin wildcards, a custom origin check, credentials, exposed headers, max age and debug logging. Its README has examples for net/http, Gin, Chi, Gorilla, Negroni, Alice, HttpRouter, Goji, Martini and Buffalo.

Problems it solves

  • add CORS headers to my Go HTTP server
  • allow cross-origin requests from my frontend to a Go API
  • handle CORS preflight OPTIONS requests in Go

Features

  • net/http handler for CORS
  • allowed origins with wildcards or a custom check function
  • preflight handling with allowed methods and headers
  • credentials, exposed headers and max age options
  • examples for Gin, Chi, Gorilla and other Go routers

Limitations

  • It no longer reflects the request's Origin when AllowedOrigins is * with AllowCredentials true; restoring that needs an AllowOriginFunc that returns true, which its README warns about.1