Skip to content

Commit

Permalink
cors: remove deprecated usage of HttpApp
Browse files Browse the repository at this point in the history
  • Loading branch information
lomigmegard authored Jul 7, 2023
1 parent 9a4c0fc commit 2d5a2bd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,50 @@
package docs.http.javadsl.server.cors;

// #cors-server-example
import org.apache.pekko.actor.typed.ActorSystem;
import org.apache.pekko.actor.typed.javadsl.Behaviors;
import org.apache.pekko.http.javadsl.Http;
import org.apache.pekko.http.javadsl.ServerBinding;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.server.*;
import org.apache.pekko.http.javadsl.server.ExceptionHandler;
import org.apache.pekko.http.javadsl.server.RejectionHandler;
import org.apache.pekko.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 org.apache.pekko.http.cors.javadsl.CorsDirectives.cors;
import static org.apache.pekko.http.cors.javadsl.CorsDirectives.corsRejectionHandler;
import static org.apache.pekko.http.javadsl.server.Directives.*;

public class CorsServerExample extends HttpApp {
public class CorsServerExample {

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 CorsServerExample app = new CorsServerExample();
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 @@ -55,7 +80,7 @@ protected Route routes() {
// Combining the two handlers only for convenience
final Function<Supplier<Route>, Route> handleErrors =
inner ->
Directives.allOf(
allOf(
s -> handleExceptions(exceptionHandler, s),
s -> handleRejections(rejectionHandler, s),
inner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,38 @@ package docs.http.scaladsl.server.cors

// #cors-server-example
import org.apache.pekko
import pekko.http.scaladsl.model.StatusCodes
import pekko.http.scaladsl.server._
import org.apache.pekko.actor.typed.ActorSystem
import org.apache.pekko.actor.typed.scaladsl.Behaviors
import org.apache.pekko.http.scaladsl.Http
import org.apache.pekko.http.scaladsl.model.StatusCodes
import org.apache.pekko.http.scaladsl.server.Directives._
import org.apache.pekko.http.scaladsl.server._

This comment has been minimized.

Copy link
@pjfanning

pjfanning Jul 8, 2023

Contributor

@mdedetrich these imports look like they should not need the 'org.apache' bit

This comment has been minimized.

Copy link
@mdedetrich

mdedetrich Jul 8, 2023

Contributor

Thanks ill fix them now


object CorsServerExample extends HttpApp {
import scala.io.StdIn
import scala.util.{ Failure, Success }

object CorsServerExample {
def main(args: Array[String]): Unit = {
CorsServerExample.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 = {
def route: Route = {
import pekko.http.cors.scaladsl.CorsDirectives._

// Your CORS settings are loaded from `application.conf`
Expand Down

0 comments on commit 2d5a2bd

Please sign in to comment.