Created
January 2, 2015 13:39
-
-
Save itang/bb03a0f0d2fc1b827580 to your computer and use it in GitHub Desktop.
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 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