diff options
author | Jakob Odersky <jakob@odersky.com> | 2017-03-02 17:01:01 -0800 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2017-03-02 19:57:42 -0800 |
commit | c4ff7eaf274eb4e2cfacdabeb65bfc43a89fdd3a (patch) | |
tree | 7c5c3f3d325820a6d99493af2f2a720803b5bd5c /crashbox-http | |
download | crashbox-ci-c4ff7eaf274eb4e2cfacdabeb65bfc43a89fdd3a.tar.gz crashbox-ci-c4ff7eaf274eb4e2cfacdabeb65bfc43a89fdd3a.tar.bz2 crashbox-ci-c4ff7eaf274eb4e2cfacdabeb65bfc43a89fdd3a.zip |
Initial commit
Diffstat (limited to 'crashbox-http')
-rw-r--r-- | crashbox-http/build.sbt | 7 | ||||
-rw-r--r-- | crashbox-http/src/main/resources/reference.conf | 4 | ||||
-rw-r--r-- | crashbox-http/src/main/scala/io/crashbox/ci/Main.scala | 55 |
3 files changed, 66 insertions, 0 deletions
diff --git a/crashbox-http/build.sbt b/crashbox-http/build.sbt new file mode 100644 index 0000000..2597b79 --- /dev/null +++ b/crashbox-http/build.sbt @@ -0,0 +1,7 @@ +import crashbox.Dependencies + +libraryDependencies ++= Seq( + Dependencies.akkaActor, + Dependencies.akkaHttp, + Dependencies.akkaHttpCore +) diff --git a/crashbox-http/src/main/resources/reference.conf b/crashbox-http/src/main/resources/reference.conf new file mode 100644 index 0000000..5d50143 --- /dev/null +++ b/crashbox-http/src/main/resources/reference.conf @@ -0,0 +1,4 @@ +crashbox { + host="[::]" + port=9000 +}
\ No newline at end of file diff --git a/crashbox-http/src/main/scala/io/crashbox/ci/Main.scala b/crashbox-http/src/main/scala/io/crashbox/ci/Main.scala new file mode 100644 index 0000000..6a61e51 --- /dev/null +++ b/crashbox-http/src/main/scala/io/crashbox/ci/Main.scala @@ -0,0 +1,55 @@ +package io.crashbox.ci + +import akka.actor.ActorSystem +import akka.http.scaladsl.Http +import akka.http.scaladsl.server.directives.Credentials +import akka.stream.ActorMaterializer +import java.text.SimpleDateFormat +import java.util.Date +import scala.util.{Failure, Success} + +object Main { + + implicit val system = ActorSystem("crashbox") + implicit val materializer = ActorMaterializer() + implicit val executionContext = system.dispatcher + + def main(args: Array[String]): Unit = { + val host = system.settings.config.getString("crashbox.host") + val port = system.settings.config.getInt("crashbox.port") + + Http(system).bindAndHandle(Route.route, host, port) onComplete { + case Success(_) => + system.log.info(s"Listening on $host:$port") + case Failure(ex) => + system.log.error(ex, s"Failed to bind to $host:$port") + system.terminate() + } + } + +} + +object Route { + import akka.http.scaladsl.server._ + import Directives._ + + private val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + + def route: Route = { + pathEndOrSingleSlash { + complete(s"Served at ${formatter.format(new Date)}") + } ~ path("secure") { + authenticateBasic(realm = "secure site", myUserPassAuthenticator) { + userName => + complete(s"The user is '$userName'") + } + } + } + + def myUserPassAuthenticator(credentials: Credentials): Option[String] = { + credentials match { + case p @ Credentials.Provided(id) if p.verify("guest") => Some(id) + case _ => None + } + } +} |