diff options
author | Jakob Odersky <jakob@driver.xyz> | 2017-10-20 14:17:21 -0700 |
---|---|---|
committer | Jakob Odersky <jakob@driver.xyz> | 2017-10-20 14:22:39 -0700 |
commit | 7df687151308cae797252e38272762f9210ed2c9 (patch) | |
tree | 0ca974b0e208f2ff358c7a20c1ebcb45b49da249 /src | |
parent | b00892d723f6dedf50dc1c1fde7d443e9c3f9497 (diff) | |
download | driver-core-1.4.4.tar.gz driver-core-1.4.4.tar.bz2 driver-core-1.4.4.zip |
Backport #77v1.4.4
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/xyz/driver/core/app.scala | 111 |
1 files changed, 58 insertions, 53 deletions
diff --git a/src/main/scala/xyz/driver/core/app.scala b/src/main/scala/xyz/driver/core/app.scala index 19eef52..49c3927 100644 --- a/src/main/scala/xyz/driver/core/app.scala +++ b/src/main/scala/xyz/driver/core/app.scala @@ -49,6 +49,7 @@ object app { scheme: String = "http", port: Int = 8080, tracer: Tracer = NoTracer)(implicit actorSystem: ActorSystem, executionContext: ExecutionContext) { + import DriverApp._ implicit private lazy val materializer = ActorMaterializer()(actorSystem) private lazy val http = Http()(actorSystem) @@ -74,59 +75,6 @@ object app { private def extractHeader(request: HttpRequest)(headerName: String): Option[String] = request.headers.find(_.name().toLowerCase === headerName).map(_.value()) - private val allowedHeaders = - Seq( - "Origin", - "X-Requested-With", - "Content-Type", - "Content-Length", - "Accept", - "X-Trace", - "Access-Control-Allow-Methods", - "Access-Control-Allow-Origin", - "Access-Control-Allow-Headers", - "Server", - "Date", - ContextHeaders.TrackingIdHeader, - ContextHeaders.TraceHeaderName, - ContextHeaders.SpanHeaderName, - ContextHeaders.StacktraceHeader, - ContextHeaders.AuthenticationTokenHeader, - "X-Frame-Options", - "X-Content-Type-Options", - "Strict-Transport-Security", - AuthProvider.SetAuthenticationTokenHeader, - AuthProvider.SetPermissionsTokenHeader - ) - - private def allowOrigin(originHeader: Option[Origin]) = - `Access-Control-Allow-Origin`( - originHeader.fold[HttpOriginRange](HttpOriginRange.*)(h => HttpOriginRange(h.origins: _*))) - - protected implicit def rejectionHandler = - RejectionHandler - .newBuilder() - .handleAll[MethodRejection] { rejections => - val methods = rejections map (_.supported) - lazy val names = methods map (_.name) mkString ", " - - options { ctx => - optionalHeaderValueByType[Origin](()) { originHeader => - respondWithHeaders(List[HttpHeader]( - Allow(methods), - `Access-Control-Allow-Methods`(methods), - allowOrigin(originHeader), - `Access-Control-Allow-Headers`(allowedHeaders: _*), - `Access-Control-Expose-Headers`(allowedHeaders: _*) - )) { - complete(s"Supported methods: $names.") - } - }(ctx) - } ~ - complete(MethodNotAllowed -> s"HTTP method not allowed, supported methods: $names!") - } - .result() - protected def bindHttp(modules: Seq[Module]): Unit = { val serviceTypes = modules.flatMap(_.routeTypes) val swaggerService = swaggerOverride(serviceTypes) @@ -386,6 +334,63 @@ object app { } } + object DriverApp { + + private val allowedHeaders = + Seq( + "Origin", + "X-Requested-With", + "Content-Type", + "Content-Length", + "Accept", + "X-Trace", + "Access-Control-Allow-Methods", + "Access-Control-Allow-Origin", + "Access-Control-Allow-Headers", + "Server", + "Date", + ContextHeaders.TrackingIdHeader, + ContextHeaders.TraceHeaderName, + ContextHeaders.SpanHeaderName, + ContextHeaders.StacktraceHeader, + ContextHeaders.AuthenticationTokenHeader, + "X-Frame-Options", + "X-Content-Type-Options", + "Strict-Transport-Security", + AuthProvider.SetAuthenticationTokenHeader, + AuthProvider.SetPermissionsTokenHeader + ) + + private def allowOrigin(originHeader: Option[Origin]) = + `Access-Control-Allow-Origin`( + originHeader.fold[HttpOriginRange](HttpOriginRange.*)(h => HttpOriginRange(h.origins: _*))) + + implicit def rejectionHandler = + RejectionHandler + .newBuilder() + .handleAll[MethodRejection] { rejections => + val methods = rejections map (_.supported) + lazy val names = methods map (_.name) mkString ", " + + options { ctx => + optionalHeaderValueByType[Origin](()) { originHeader => + respondWithHeaders(List[HttpHeader]( + Allow(methods), + `Access-Control-Allow-Methods`(methods), + allowOrigin(originHeader), + `Access-Control-Allow-Headers`(allowedHeaders: _*), + `Access-Control-Expose-Headers`(allowedHeaders: _*) + )) { + complete(s"Supported methods: $names.") + } + }(ctx) + } ~ + complete(MethodNotAllowed -> s"HTTP method not allowed, supported methods: $names!") + } + .result() + + } + trait Module { val name: String def route: Route |