Spray(7)REST API Project - Routing
5. How to deal with routing
First start class Boot.scala
package com.sillycat.easysprayrestserver
import spray.can.server.SprayCanHttpServerApp
import akka.actor.Props
import com.sillycat.easysprayrestserver.bootstrap.Bootstrap
import com.sillycat.easysprayrestserver.actor.URLRouterActor
object Boot extends App with Bootstrap with SprayCanHttpServerApp {
valhandler = system.actorOf(Props[URLRouterActor])
newHttpServer(handler) ! Bind(interface = serverAddress, port = serverPort)
}
Bootstrap.scala to prepare some parameters
package com.sillycat.easysprayrestserver.bootstrap
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
trait Bootstrap {
//implicit val system = ActorSystem("RESTAPIService")
valconfig = ConfigFactory.load()
valenv = config.getString("build.env")
varserverAddress: String = config.getString("server.address")
varserverPort: Int = config.getInt("server.port")
if (env != null && env != "") {
serverAddress = if (config.getString("environment." + env + ".server.address") != null) config.getString("environment." + env + ".server.address") elseserverAddress
serverPort = if (config.getInt("environment." + env + ".server.port") != 0) config.getInt("environment." + env + ".server.port") elseserverPort
}
}
URLRouterActor to manage the URL resources
package com.sillycat.easysprayrestserver.actor
import akka.actor.{ Props, Actor }
import spray.routing._
import spray.routing.directives._
import spray.util.LoggingContext
import spray.http.StatusCodes._
import spray.httpx.SprayJsonSupport._
import shapeless._
class URLRouterActor extends Actor with URLRouterService {
def actorRefFactory = context
def receive = runRoute(route)
}
trait URLRouterService extends HttpService {
def route = {
pathPrefix(Version / BrandCode) { (apiVersion, brandCode) =>
path("resource" / "all") {
get {
complete {
"Morning, guest. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "admin-only") {
get {
complete {
"Morning, admin-only. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "customer-only") {
get {
complete {
"Morning, customer-only. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "better-admin") {
get {
complete {
"Morning, better-admin. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "better-customer") {
get {
complete {
"Morning, better-customer. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
}
}
}
valVersion = PathMatcher("""v([0-9]+)""".r)
.flatMap {
casestring :: HNil => {
try Some(java.lang.Integer.parseInt(string) :: HNil)
catch {
case _: NumberFormatException => None
}
}
}
valBrandCode = PathElement
}
Visit the URL as follow to verify that.
http://localhost:9000/v1/sillycat/resource/better-customer
6. How to Deal with Auth
7. How to work with DB
come soon...
8. How to Work with Actor
come soon…
9. How to do Validation
come soon...
10. How to Work with Logback
come soon…
Tips:
1. Add formatter to plugins
project/plugins.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.0.1")
References:
http://www.gtan.com/akka_doc/scala/routing.html
https://github.com/cakesolutions/spray-auth-example
http://spray.io/documentation/spray-routing/
https://github.com/spray/spray/wiki/Authentication-Authorization
https://github.com/spray/spray/wiki/Configuration
https://github.com/spray/spray/wiki
Spray(7)REST API Project - Routing
猜你喜欢
转载自sillycat.iteye.com/blog/1858282
今日推荐
周排行