Skip to content

Commit

Permalink
Remove deprecated usage of HttpApp in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lomigmegard committed Jul 22, 2023
1 parent 9097b97 commit f746e62
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> 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<ServerBinding> 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`

Expand All @@ -51,7 +76,7 @@ protected Route routes() {
.build();

// Combining the two handlers only for convenience
final Function<Supplier<Route>, Route> handleErrors = inner -> Directives.allOf(
final Function<Supplier<Route>, Route> handleErrors = inner -> allOf(
s -> handleExceptions(exceptionHandler, s),
s -> handleRejections(rejectionHandler, s),
inner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down

0 comments on commit f746e62

Please sign in to comment.