Skip to content

Instantly share code, notes, and snippets.

@itang
Created January 2, 2015 13:39
Show Gist options
  • Save itang/bb03a0f0d2fc1b827580 to your computer and use it in GitHub Desktop.
Save itang/bb03a0f0d2fc1b827580 to your computer and use it in GitHub Desktop.
import akka.actor.ActorSystem
import akka.stream.FlowMaterializer
import akka.http.Http
import akka.http.model._
import akka.http.model.HttpMethods._
object Main {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = FlowMaterializer()
val serverBinding = Http(system).bind(interface = "localhost", port = 8080)
val requestHandler: HttpRequest ⇒ HttpResponse = {
case HttpRequest(GET, Uri.Path("/"), _, _, _) ⇒
HttpResponse(
entity = HttpEntity(MediaTypes.`text/html`,
"<html><body>Hello world!</body></html>"))
case HttpRequest(GET, Uri.Path("/ping"), _, _, _) ⇒ HttpResponse(entity = "PONG!")
case HttpRequest(GET, Uri.Path("/crash"), _, _, _) ⇒ sys.error("BOOM!")
case _: HttpRequest ⇒ HttpResponse(404, entity = "Unknown resource!")
}
serverBinding.connections.foreach { connection ⇒
connection handleWithSyncHandler requestHandler
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment