`
sillycat
  • 浏览: 2542882 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spray(7)REST API Project - Routing

 
阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics