├── project ├── plugins.sbt └── build.properties ├── .gitignore └── src ├── main └── scala │ └── com │ └── madhukaraphatak │ └── akkahttp │ ├── Models.scala │ ├── AkkaHttpHelloWorld.scala │ ├── testable │ ├── RestServer.scala │ └── RestService.scala │ └── AkkaJsonParsing.scala └── test └── scala └── com └── madhukaraphatak └── akkahttp └── testable └── RestSpec.scala /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | logLevel := Level.Warn -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 0.13.6 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea/ 3 | *.iml 4 | target/ 5 | dependency-reduced-pom.xml 6 | -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akkahttp/Models.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akkahttp 2 | 3 | import spray.json.DefaultJsonProtocol 4 | 5 | /** 6 | * Created by madhu on 8/11/15. 7 | */ 8 | object Models { 9 | case class Customer(name: String) 10 | object ServiceJsonProtoocol extends DefaultJsonProtocol { 11 | implicit val customerProtocol = jsonFormat1(Customer) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akkahttp/AkkaHttpHelloWorld.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akkahttp 2 | 3 | 4 | import akka.actor.ActorSystem 5 | import akka.stream.ActorMaterializer 6 | import akka.http.scaladsl.Http 7 | import akka.http.scaladsl.server.Directives._ 8 | 9 | /** 10 | * Created by madhu on 8/11/15. 11 | */ 12 | object AkkaHttpHelloWorld { 13 | 14 | def main(args: Array[String]) { 15 | 16 | implicit val actorSystem = ActorSystem("system") 17 | implicit val actorMaterializer = ActorMaterializer() 18 | 19 | 20 | val route = 21 | pathSingleSlash { 22 | get { 23 | complete { 24 | "Hello world" 25 | } 26 | } 27 | } 28 | Http().bindAndHandle(route,"localhost",8080) 29 | 30 | println("server started at 8080") 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akkahttp/testable/RestServer.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akkahttp.testable 2 | 3 | import akka.actor.ActorSystem 4 | import akka.http.scaladsl.Http 5 | import akka.stream.ActorMaterializer 6 | 7 | /** 8 | * Created by madhu on 8/11/15. 9 | */ 10 | 11 | 12 | class RestServer(implicit val system:ActorSystem, 13 | implicit val materializer:ActorMaterializer) extends RestService{ 14 | def startServer(address:String, port:Int) = { 15 | Http().bindAndHandle(route,address,port) 16 | } 17 | } 18 | 19 | object RestServer { 20 | 21 | def main(args: Array[String]) { 22 | 23 | implicit val actorSystem = ActorSystem("rest-server") 24 | implicit val materializer = ActorMaterializer() 25 | 26 | val server = new RestServer() 27 | server.startServer("localhost",8080) 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/test/scala/com/madhukaraphatak/akkahttp/testable/RestSpec.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akkahttp.testable 2 | 3 | import akka.http.scaladsl.testkit.ScalatestRouteTest 4 | import akka.util.ByteString 5 | import org.scalatest.{Matchers, WordSpec} 6 | import akka.http.scaladsl.model._ 7 | 8 | 9 | /** 10 | * Created by madhu on 8/11/15. 11 | */ 12 | class RestSpec extends WordSpec with Matchers with ScalatestRouteTest with RestService{ 13 | "Customer API" should { 14 | "Posting to /customer should add the customer" in { 15 | 16 | val jsonRequest = ByteString( 17 | s""" 18 | |{ 19 | | "name":"test" 20 | |} 21 | """.stripMargin) 22 | 23 | val postRequest = HttpRequest( 24 | HttpMethods.POST, 25 | uri = "/customer", 26 | entity = HttpEntity(MediaTypes.`application/json`, jsonRequest)) 27 | 28 | 29 | postRequest ~> route ~> check { 30 | status.isSuccess() shouldEqual true 31 | } 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akkahttp/testable/RestService.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akkahttp.testable 2 | 3 | import java.util.concurrent.ConcurrentLinkedDeque 4 | 5 | import akka.actor.ActorSystem 6 | import akka.http.scaladsl.server.Directives._ 7 | import akka.stream.ActorMaterializer 8 | import com.madhukaraphatak.akkahttp.Models.{ServiceJsonProtoocol, Customer} 9 | import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ 10 | import spray.json.JsArray 11 | import scala.collection.JavaConverters._ 12 | import spray.json.{JsArray, pimpAny, DefaultJsonProtocol} 13 | import spray.json.DefaultJsonProtocol._ 14 | 15 | 16 | /** 17 | * Created by madhu on 8/11/15. 18 | */ 19 | trait RestService { 20 | implicit val system:ActorSystem 21 | implicit val materializer:ActorMaterializer 22 | 23 | val list = new ConcurrentLinkedDeque[Customer]() 24 | 25 | import ServiceJsonProtoocol._ 26 | val route = 27 | path("customer") { 28 | post { 29 | entity(as[Customer]) { 30 | customer => complete { 31 | list.add(customer) 32 | s"got customer with name ${customer.name}" 33 | } 34 | } 35 | } ~ 36 | get { 37 | complete { 38 | list.asScala 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/scala/com/madhukaraphatak/akkahttp/AkkaJsonParsing.scala: -------------------------------------------------------------------------------- 1 | package com.madhukaraphatak.akkahttp 2 | 3 | import java.util.concurrent.ConcurrentLinkedDeque 4 | 5 | import akka.actor.ActorSystem 6 | import akka.http.scaladsl.Http 7 | import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ 8 | import akka.http.scaladsl.server.Directives._ 9 | import akka.stream.ActorMaterializer 10 | import com.madhukaraphatak.akkahttp.Models.{Customer, ServiceJsonProtoocol} 11 | import spray.json.DefaultJsonProtocol._ 12 | import scala.collection.JavaConverters._ 13 | 14 | /** 15 | * Created by madhu on 8/11/15. 16 | */ 17 | object AkkaJsonParsing { 18 | 19 | 20 | 21 | def main(args: Array[String]) { 22 | 23 | implicit val actorSystem = ActorSystem("rest-api") 24 | 25 | implicit val actorMaterializer = ActorMaterializer() 26 | 27 | val list = new ConcurrentLinkedDeque[Customer]() 28 | 29 | import ServiceJsonProtoocol.customerProtocol 30 | val route = 31 | path("customer") { 32 | post { 33 | entity(as[Customer]) { 34 | customer => complete { 35 | list.add(customer) 36 | s"got customer with name ${customer.name}" 37 | } 38 | } 39 | } ~ 40 | get { 41 | complete { 42 | list.asScala 43 | } 44 | } 45 | } 46 | 47 | Http().bindAndHandle(route, "localhost", 8080) 48 | } 49 | 50 | 51 | } 52 | --------------------------------------------------------------------------------