Skip to content

Instantly share code, notes, and snippets.

@amolbrid
Created February 13, 2015 16:00
Show Gist options
  • Save amolbrid/07d288994ccacc171594 to your computer and use it in GitHub Desktop.
Save amolbrid/07d288994ccacc171594 to your computer and use it in GitHub Desktop.
Users route
package manta
import manta.routes.Users
import spray.routing.HttpService
import scala.concurrent.ExecutionContext.Implicits.global
trait SecuredRoutes extends HttpService with Users with TokenAuthenticator {
val routes = authenticate(basicTokenAuth) { implicit user =>
usersRoute
}
}
package manta
import akka.actor.Actor
import com.collective.serving.util.pimpConfig
import com.typesafe.config.Config
/*
* We don't implement our route structure directly in the service actor because
* we want to be able to test it independently, without having to spin up an actor.
*/
class ServiceActor extends Actor with InfoService with SecuredRoutes {
private val config: Config = context.system.settings.config
val slowResponseTimer = new SlowResponseTimer(config.getFiniteDuration("manta.metrics.slow-response-threshold"))
/*
* The HttpService trait defines only one abstract member, which
* connects the services environment to the enclosing actor or test.
*/
def actorRefFactory = context
/*
* This actor only runs our route, but you could add
* other things here, like request stream processing
* or timeout handling.
*/
// def receive = runRoute(slowResponseTimer.time(infoRoute ~ usersRoute))
def receive = runRoute(infoRoute ~ routes)
}
package manta.routes
import manta.models.User
import spray.routing.HttpService
trait Users extends HttpService {
def usersRoute(implicit user: User) = {
pathPrefix("users") {
pathEndOrSingleSlash {
get {
val service = new UserService
complete(service.list)
} ~
post {
complete("ok")
}
} ~
path(IntNumber) { id =>
get {
service = new UserService
complete(service.get(id))
} ~ put {
complete("ok")
} ~ delete {
complete("ok")
}
} ~
path("current") {
get {
complete("ok: " + user.emailAddress)
}
}
}
}
}
package manta.rest
import manta.models.User
class UsersService(implicit user: User) {
def list() = { }
def get(id: Int) = { }
def create(user: User) = { }
def update(user: User) = { }
def delete(id: Int) = { }
}
package manta.routes
import manta.models.User
import org.scalatest.{FreeSpec, Matchers}
import spray.http.StatusCodes
import spray.testkit.ScalatestRouteTest
class UsersSpec extends FreeSpec with ScalatestRouteTest with Matchers with Users {
def actorRefFactory = system
implicit val user: User = new User(1, "a@b", Some("asddfg"))
"Current user" - {
Get("/users/current") ~> usersRoute ~> check {
handled shouldEqual true
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual "ok: a@b"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment