Skip to content

Instantly share code, notes, and snippets.

@gabfssilva
Created January 11, 2019 14:12
Show Gist options
  • Save gabfssilva/d737c1348b9ff4dce5e63b3a2ce7ea44 to your computer and use it in GitHub Desktop.
Save gabfssilva/d737c1348b9ff4dce5e63b3a2ce7ea44 to your computer and use it in GitHub Desktop.
akka http showcase
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.{Directives, Route}
import akka.stream.ActorMaterializer
import scala.concurrent.ExecutionContext
abstract class AkkaHttpApp(host: String = "localhost", port: Int = 8080) extends App with Directives {
implicit val sys: ActorSystem = ActorSystem()
implicit val mat: ActorMaterializer = ActorMaterializer()
implicit val ec: ExecutionContext = sys.dispatcher
def routes: Route
Http().bindAndHandle(routes, host, port).onComplete(println)
}
import akka.http.scaladsl.server.Route
object SimpleServer extends AkkaHttpApp {
def helloWorld = path("hello") {
complete("world!")
}
def getOrPost = get | post
def calc(p: String)(f: (Int, Int) => Int) =
(path(p) & getOrPost & parameter('x.as[Int], 'y.as[Int])) { (x, y) =>
complete(s"The result is ${f(x, y)}")
}
def multiply = calc("multiply")(_ * _)
def sum = calc("sum")(_ + _)
def sub = calc("sub")(_ - _)
def routes: Route = pathPrefix("v1") {
helloWorld ~ sum ~ sub ~ multiply
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment