2015-05-21 17:17:55 +02:00
|
|
|
/*
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-05-21 17:17:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.http.scaladsl.server
|
|
|
|
|
|
|
|
|
|
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport
|
2015-06-03 17:33:18 +09:00
|
|
|
import akka.http.scaladsl.server.directives.Credentials
|
2015-10-23 06:41:02 -07:00
|
|
|
import akka.stream.scaladsl._
|
2015-05-21 17:17:55 +02:00
|
|
|
import com.typesafe.config.{ ConfigFactory, Config }
|
|
|
|
|
import akka.actor.ActorSystem
|
2015-06-23 18:28:53 +02:00
|
|
|
import akka.stream.ActorMaterializer
|
2015-05-21 17:17:55 +02:00
|
|
|
import akka.http.scaladsl.Http
|
|
|
|
|
|
|
|
|
|
object TestServer extends App {
|
|
|
|
|
val testConf: Config = ConfigFactory.parseString("""
|
|
|
|
|
akka.loglevel = INFO
|
2016-01-06 23:47:41 +01:00
|
|
|
akka.log-dead-letters = off
|
|
|
|
|
akka.stream.materializer.debug.fuzzing-mode = off
|
|
|
|
|
""")
|
2015-05-21 17:17:55 +02:00
|
|
|
implicit val system = ActorSystem("ServerTest", testConf)
|
|
|
|
|
import system.dispatcher
|
2015-06-23 18:28:53 +02:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-05-21 17:17:55 +02:00
|
|
|
|
|
|
|
|
import ScalaXmlSupport._
|
|
|
|
|
import Directives._
|
|
|
|
|
|
|
|
|
|
def auth: AuthenticatorPF[String] = {
|
2015-06-03 17:33:18 +09:00
|
|
|
case p @ Credentials.Provided(name) if p.verify(name + "-password") ⇒ name
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val bindingFuture = Http().bindAndHandle({
|
|
|
|
|
get {
|
|
|
|
|
path("") {
|
|
|
|
|
complete(index)
|
|
|
|
|
} ~
|
|
|
|
|
path("secure") {
|
|
|
|
|
authenticateBasicPF("My very secure site", auth) { user ⇒
|
|
|
|
|
complete(<html><body>Hello <b>{ user }</b>. Access has been granted!</body></html>)
|
|
|
|
|
}
|
|
|
|
|
} ~
|
|
|
|
|
path("ping") {
|
|
|
|
|
complete("PONG!")
|
|
|
|
|
} ~
|
|
|
|
|
path("crash") {
|
|
|
|
|
complete(sys.error("BOOM!"))
|
|
|
|
|
}
|
2015-05-17 15:39:12 +02:00
|
|
|
} ~ pathPrefix("inner")(getFromResourceDirectory("someDir"))
|
2015-05-21 17:17:55 +02:00
|
|
|
}, interface = "localhost", port = 8080)
|
|
|
|
|
|
|
|
|
|
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
|
|
|
|
|
Console.readLine()
|
|
|
|
|
|
2016-01-17 15:48:52 +01:00
|
|
|
bindingFuture.flatMap(_.unbind()).onComplete(_ ⇒ system.terminate())
|
2015-05-21 17:17:55 +02:00
|
|
|
|
|
|
|
|
lazy val index =
|
|
|
|
|
<html>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>Say hello to <i>akka-http-core</i>!</h1>
|
|
|
|
|
<p>Defined resources:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="/ping">/ping</a></li>
|
|
|
|
|
<li><a href="/secure">/secure</a> Use any username and '<username>-password' as credentials</li>
|
|
|
|
|
<li><a href="/crash">/crash</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
}
|