diff --git a/akka-http-cors-example/src/main/java/ch/megard/akka/http/cors/javadsl/CorsServer.java b/akka-http-cors-example/src/main/java/ch/megard/akka/http/cors/javadsl/CorsServer.java index 7211fd1..ca6182e 100644 --- a/akka-http-cors-example/src/main/java/ch/megard/akka/http/cors/javadsl/CorsServer.java +++ b/akka-http-cors-example/src/main/java/ch/megard/akka/http/cors/javadsl/CorsServer.java @@ -17,28 +17,53 @@ package ch.megard.akka.http.cors.javadsl; +import akka.actor.typed.ActorSystem; +import akka.actor.typed.javadsl.Behaviors; +import akka.http.javadsl.Http; +import akka.http.javadsl.ServerBinding; import akka.http.javadsl.model.StatusCodes; -import akka.http.javadsl.server.*; +import akka.http.javadsl.server.ExceptionHandler; +import akka.http.javadsl.server.RejectionHandler; +import akka.http.javadsl.server.Route; import java.util.NoSuchElementException; -import java.util.concurrent.ExecutionException; +import java.util.concurrent.CompletionStage; import java.util.function.Function; import java.util.function.Supplier; +import static akka.http.javadsl.server.Directives.*; import static ch.megard.akka.http.cors.javadsl.CorsDirectives.cors; import static ch.megard.akka.http.cors.javadsl.CorsDirectives.corsRejectionHandler; /** * Example of a Java HTTP server using the CORS directive. */ -public class CorsServer extends HttpApp { +public class CorsServer { + + public static void main(String[] args) throws Exception { + final ActorSystem system = ActorSystem.create(Behaviors.empty(), "cors-server"); - public static void main(String[] args) throws ExecutionException, InterruptedException { final CorsServer app = new CorsServer(); - app.startServer("127.0.0.1", 9000); + + final CompletionStage futureBinding = + Http.get(system).newServerAt("localhost", 8080).bind(app.createRoute()); + + futureBinding.whenComplete((binding, exception) -> { + if (binding != null) { + system.log().info("Server online at http://localhost:8080/\nPress RETURN to stop..."); + } else { + system.log().error("Failed to bind HTTP endpoint, terminating system", exception); + system.terminate(); + } + }); + + System.in.read(); // let it run until user presses return + futureBinding + .thenCompose(ServerBinding::unbind) + .thenAccept(unbound -> system.terminate()); } - protected Route routes() { + private Route createRoute() { // Your CORS settings are loaded from `application.conf` @@ -51,7 +76,7 @@ protected Route routes() { .build(); // Combining the two handlers only for convenience - final Function, Route> handleErrors = inner -> Directives.allOf( + final Function, Route> handleErrors = inner -> allOf( s -> handleExceptions(exceptionHandler, s), s -> handleRejections(rejectionHandler, s), inner diff --git a/akka-http-cors-example/src/main/scala/ch/megard/akka/http/cors/scaladsl/CorsServer.scala b/akka-http-cors-example/src/main/scala/ch/megard/akka/http/cors/scaladsl/CorsServer.scala index dc517bc..fee1abd 100644 --- a/akka-http-cors-example/src/main/scala/ch/megard/akka/http/cors/scaladsl/CorsServer.scala +++ b/akka-http-cors-example/src/main/scala/ch/megard/akka/http/cors/scaladsl/CorsServer.scala @@ -16,17 +16,40 @@ package ch.megard.akka.http.cors.scaladsl +import akka.actor.typed.ActorSystem +import akka.actor.typed.scaladsl.Behaviors +import akka.http.scaladsl.Http import akka.http.scaladsl.model.StatusCodes +import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server._ +import scala.io.StdIn +import scala.util.{Failure, Success} + /** Example of a Scala HTTP server using the CORS directive. */ -object CorsServer extends HttpApp { +object CorsServer { def main(args: Array[String]): Unit = { - CorsServer.startServer("127.0.0.1", 9000) + implicit val system: ActorSystem[Nothing] = ActorSystem(Behaviors.empty, "cors-server") + import system.executionContext + + val futureBinding = Http().newServerAt("localhost", 8080).bind(route) + + futureBinding.onComplete { + case Success(_) => + system.log.info("Server online at http://localhost:8080/\nPress RETURN to stop...") + case Failure(exception) => + system.log.error("Failed to bind HTTP endpoint, terminating system", exception) + system.terminate() + } + + StdIn.readLine() // let it run until user presses return + futureBinding + .flatMap(_.unbind()) + .onComplete(_ => system.terminate()) } - protected def routes: Route = { + private def route: Route = { import CorsDirectives._ // Your CORS settings are loaded from `application.conf` diff --git a/build.sbt b/build.sbt index f33b9f1..a423cd5 100644 --- a/build.sbt +++ b/build.sbt @@ -71,7 +71,8 @@ lazy val `akka-http-cors-example` = project .settings(commonSettings) .settings(dontPublishSettings) .settings( - libraryDependencies += "com.typesafe.akka" %% "akka-stream" % akkaVersion cross CrossVersion.for3Use2_13 + libraryDependencies += "com.typesafe.akka" %% "akka-stream" % akkaVersion cross CrossVersion.for3Use2_13, + libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion cross CrossVersion.for3Use2_13 // libraryDependencies += "ch.megard" %% "akka-http-cors" % version.value )