Skip to content

Commit

Permalink
Merge pull request #3 from tkqubo/issue#1
Browse files Browse the repository at this point in the history
PR to close #1
  • Loading branch information
tkqubo committed Feb 3, 2016
2 parents 012b49b + a742782 commit 4c9edf3
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/github/qubo/seed/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import akka.actor.{ActorRef, ActorSystem, Props}
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import com.github.qubo.seed.router.ApiRouterActor
import com.github.qubo.seed.routes.ApiRouterActor
import com.github.qubo.seed.utils.Config
import Config.app
import spray.can.Http
Expand Down
57 changes: 0 additions & 57 deletions src/main/scala/com/github/qubo/seed/SwaggerDefinition.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.qubo.seed.persistence
package com.github.qubo.seed.persistences

import io.swagger.annotations.ApiModel
import slick.dbio.Effect.Read
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.qubo.seed.router
package com.github.qubo.seed.routes

import org.slf4j.LoggerFactory
import spray.httpx.SprayJsonSupport
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.github.qubo.seed.router
package com.github.qubo.seed.routes

import java.util.NoSuchElementException

import akka.actor.{Actor, ActorContext, ActorLogging}
import com.github.qubo.seed.swagger.{SwaggerDefinition, SwaggerDefinitionConfig}
import spray.http.MediaTypes._
import spray.http.StatusCodes
import spray.http.{AllOrigins, StatusCodes}
import spray.httpx.SprayJsonSupport
import spray.json.DefaultJsonProtocol
import spray.routing.{ExceptionHandler, HttpService, Route}
Expand All @@ -19,6 +19,7 @@ class ApiRouterActor
with DefaultJsonProtocol
with SprayJsonSupport
with UserApi
with CorsHelper
with HttpService {
def actorRefFactory: ActorContext = context
implicit val ec: ExecutionContext = actorRefFactory.dispatcher
Expand All @@ -28,7 +29,7 @@ class ApiRouterActor
)

def receive: Receive = runRoute(
respondWithMediaType(`application/json`) {
(cors(AllOrigins) & respondWithMediaType(`application/json`)) {
apiRoute ~ swaggerRoute
} ~ swaggerUiRoute
)
Expand Down
55 changes: 55 additions & 0 deletions src/main/scala/com/github/qubo/seed/routes/CorsHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.qubo.seed.routes


import spray.http.HttpHeaders._
import spray.http.HttpMethods._
import spray.http._
import spray.routing._
import spray.util._

trait CorsHelper {
this: HttpService =>
val MaxAge = 1728000

def p3p(value: String = """CP="CAO PSA OUR""""): Directive0 =
mapHttpResponseHeaders(_ ++ List(RawHeader("P3P", value)))

def cors(origins: Seq[String]): Directive0 =
cors(SomeOrigins(origins.map(HttpOrigin(_))))

def cors(origin: String): Directive0 =
if (origin == "*") {
cors(AllOrigins)
} else {
cors(SomeOrigins(Seq(HttpOrigin(origin))))
}

def cors(origins: AllowedOrigins): Directive0 = mapRequestContext { ctx =>
val originHeader = if (origins == AllOrigins) {
ctx.request.headers.findByType[`Origin`]
} else {
None
}
val resOrigins = originHeader.map { case Origin(origin) => SomeOrigins(origin) }.getOrElse(origins)
ctx.withRouteResponseHandling({
//It is an option request for a resource that responds to some other method
case Rejected(x) if ctx.request.method.equals(HttpMethods.OPTIONS) =>
ctx.complete(HttpResponse().withHeaders(
allowOriginHeader(resOrigins) ++ fixedCorsHeaders
))
}).withHttpResponseHeadersMapped { headers =>
allowOriginHeader(resOrigins) ++ fixedCorsHeaders ++ headers
}
} & p3p()

private def fixedCorsHeaders: List[HttpHeader] =
List(
`Access-Control-Allow-Methods`(GET, POST, PATCH, PUT, DELETE, OPTIONS),
`Access-Control-Allow-Headers`("Content-Type,Accept-Language,Authorization"),
`Access-Control-Allow-Credentials`(true),
`Access-Control-Max-Age`(MaxAge)
)

private def allowOriginHeader(origins: AllowedOrigins): List[HttpHeader] =
List(`Access-Control-Allow-Origin`(origins))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.qubo.seed.router
package com.github.qubo.seed.routes

import javax.ws.rs.Path

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.qubo.seed.router
package com.github.qubo.seed.routes

import javax.ws.rs.{GET, Path, PathParam}

import com.github.qubo.seed.persistence.User
import com.github.qubo.seed.persistences.User
import com.github.qubo.seed.utils.Config
import io.swagger.annotations._
import spray.routing.{Route, StandardRoute}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.qubo.seed.persistence
package com.github.qubo.seed.persistences

import org.specs2.mutable.Specification
import org.specs2.specification.process.RandomSequentialExecution
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.qubo.seed.persistence
package com.github.qubo.seed.persistences

import scalaz._, Scalaz._

Expand Down

0 comments on commit 4c9edf3

Please sign in to comment.