Created
April 25, 2015 15:19
-
-
Save mfirry/68cdbd13ce84bd12aa56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.Route | |
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow | |
import com.typesafe.config.ConfigFactory | |
import akka.stream.ActorFlowMaterializer | |
import akka.stream.scaladsl._ | |
import scala.concurrent._ | |
object Main extends App { | |
val config = ConfigFactory.load() | |
implicit val system = ActorSystem.create() | |
implicit val executionContext = system.dispatcher | |
implicit val materializer = ActorFlowMaterializer() | |
lazy val route = | |
get { | |
path("json") { | |
import spray.json._ | |
import spray.json.DefaultJsonProtocol._ | |
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ | |
complete(JsObject("message" -> JsString("Hello, World!"))) | |
} ~ | |
path("plaintext") { | |
complete("Hello, World!") | |
} | |
} | |
val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] = | |
Http(system).bind(interface = "localhost", port = 8080) | |
val bindingFuture: Future[Http.ServerBinding] = | |
serverSource.to(Sink.foreach { connection => | |
connection handleWith route | |
}).run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
name := """akka-http-example"""
version := "0.0.1-SNAPSHOT"
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http-core-experimental" % "1.0-RC1",
"com.typesafe.akka" %% "akka-stream-experimental" % "1.0-RC1",
"com.typesafe.akka" % "akka-http-spray-json-experimental_2.11" % "1.0-RC1"
)