-
-
Save hsanchez/72cf374e4bd70c3584c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.agilogy.spray.cors | |
import spray.http.{HttpMethods, HttpMethod, HttpResponse, AllOrigins} | |
import spray.http.HttpHeaders._ | |
import spray.http.HttpMethods._ | |
import spray.routing._ | |
// see also https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS | |
trait CORSSupport { | |
this: HttpService => | |
private val allowOriginHeader = `Access-Control-Allow-Origin`(AllOrigins) | |
private val optionsCorsHeaders = List( | |
`Access-Control-Allow-Headers`("Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Accept-Language, Host, Referer, User-Agent"), | |
`Access-Control-Max-Age`(1728000)) | |
def cors[T]: Directive0 = mapRequestContext { ctx => ctx.withRouteResponseHandling({ | |
//It is an option requeset for a resource that responds to some other method | |
case Rejected(x) if (ctx.request.method.equals(HttpMethods.OPTIONS) && !x.filter(_.isInstanceOf[MethodRejection]).isEmpty) => { | |
val allowedMethods: List[HttpMethod] = x.filter(_.isInstanceOf[MethodRejection]).map(rejection=> { | |
rejection.asInstanceOf[MethodRejection].supported | |
}) | |
ctx.complete(HttpResponse().withHeaders( | |
`Access-Control-Allow-Methods`(OPTIONS, allowedMethods :_*) :: allowOriginHeader :: | |
optionsCorsHeaders | |
)) | |
} | |
}).withHttpResponseHeadersMapped { headers => | |
allowOriginHeader :: headers | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val routes: Route = | |
cors { | |
path("hello") { | |
get { | |
complete { | |
"GET" | |
} | |
} ~ | |
put { | |
complete { | |
"PUT" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment