Created
June 23, 2015 15:54
-
-
Save mattroberts297/a104f1abde2fdc7f05d9 to your computer and use it in GitHub Desktop.
akka-http 1.0-RC3
This file contains 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
lazy val root = (project in file(".")).settings( | |
name := "akka-http-example", | |
version := "0.1.0", | |
scalaVersion := "2.11.6", | |
libraryDependencies ++= Seq( | |
"com.typesafe.akka" %% "akka-stream-experimental" % "1.0-RC3", | |
"com.typesafe.akka" %% "akka-http-core-experimental" % "1.0-RC3", | |
"com.typesafe.akka" %% "akka-http-experimental" % "1.0-RC3", | |
"com.typesafe.akka" %% "akka-http-testkit-experimental" % "1.0-RC3" % "test" | |
) | |
) |
This file contains 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
import java.io.File | |
import java.util.UUID | |
import akka.actor.ActorSystem | |
import akka.http.scaladsl.model.headers.Location | |
import akka.http.scaladsl.model.{HttpEntity, StatusCodes, HttpResponse} | |
import akka.stream.{FlowMaterializer, ActorFlowMaterializer} | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.server.Directives._ | |
import akka.stream.io._ | |
import scala.io.StdIn | |
import scala.util.{Failure, Success} | |
object Main extends App { | |
implicit val system = ActorSystem("akka-http-example-system") | |
import system.dispatcher | |
implicit val materializer: FlowMaterializer = ActorFlowMaterializer() | |
val route = | |
pathPrefix("files") { | |
pathEndOrSingleSlash { | |
post { | |
entity(as[HttpEntity]) { entity => | |
onComplete { | |
val uuid = UUID.randomUUID() | |
val file = SynchronousFileSink(new File(uuid.toString)) | |
entity.dataBytes.runWith(file).map(_ => uuid) | |
} { | |
case Success(uuid) => complete(HttpResponse(StatusCodes.Created, List(Location(s"/files/$uuid")))) | |
case Failure(ex) => complete(StatusCodes.InternalServerError) | |
} | |
} | |
} ~ | |
get { | |
complete("TODO: Get /files") | |
} | |
} ~ | |
pathSuffix(RestPath) { path => | |
get { | |
val pathWithoutSlash = if (path.endsWithSlash) path.reverse.tail.reverse else path | |
val uuid = UUID.fromString(pathWithoutSlash.toString()) | |
getFromFile(new File(uuid.toString)) | |
} ~ | |
put { | |
complete("TODO: Put /files/:id") | |
} | |
} | |
} | |
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080) | |
bindingFuture.map { binding => | |
val address = binding.localAddress | |
val hostName = address.getHostName | |
val port = address.getPort | |
println(s"Server online at http://$hostName:$port") | |
println("Press RETURN to stop...") | |
StdIn.readLine() | |
binding.unbind().onComplete(_ => system.shutdown()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment