Skip to content

Instantly share code, notes, and snippets.

@alexandru
Created May 9, 2013 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexandru/5545852 to your computer and use it in GitHub Desktop.
Save alexandru/5545852 to your computer and use it in GitHub Desktop.
For serving CORS headers in responses.
import play.api.mvc._
trait CorsSupport { self: Controller =>
def CorsAction(cb: => Result): Action[AnyContent] =
Action(cb.withHeaders(actionCorsHeaders: _*))
def CorsAction(cb: Request[AnyContent] => Result): Action[AnyContent] =
Action { request =>
cb(request).withHeaders(actionCorsHeaders: _*)
}
def CorsAction[A](bp: BodyParser[A])(f: Request[A] => Result): Action[A] =
Action(bp) { request =>
f(request).withHeaders(actionCorsHeaders: _*)
}
private[this] val actionCorsHeaders = Array(
"Access-Control-Allow-Methods" -> "GET, POST, OPTIONS, HEAD, PUT, DELETE",
"Access-Control-Allow-Credentials" -> "true",
"Access-Control-Allow-Headers" -> "ORIGIN, CONTENT-TYPE, X-REQUESTED-WITH, *",
"Access-Control-Allow-Origin" -> "*",
"Access-Control-Max-Age" -> "30000"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment