/plugin-tester-java/src/main/java/example/myapp/helloworld/AuthenticatedGreeterServer.java

https://github.com/akka/akka-grpc · Java · 91 lines · 56 code · 17 blank · 18 comment · 2 complexity · 23b07bc0e9080f1b9f3b0c3e0e753800 MD5 · raw file

  1. /*
  2. * Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package example.myapp.helloworld;
  5. import java.util.concurrent.CompletionStage;
  6. import akka.http.javadsl.model.StatusCodes;
  7. import com.typesafe.config.Config;
  8. import com.typesafe.config.ConfigFactory;
  9. import akka.actor.ActorSystem;
  10. import akka.grpc.javadsl.RouteUtils;
  11. import akka.http.javadsl.ConnectHttp;
  12. import akka.http.javadsl.Http;
  13. import akka.http.javadsl.ServerBinding;
  14. import akka.http.javadsl.model.HttpRequest;
  15. import akka.http.javadsl.model.HttpResponse;
  16. import akka.http.javadsl.server.Route;
  17. import akka.japi.Function;
  18. import akka.stream.ActorMaterializer;
  19. import akka.stream.Materializer;
  20. import example.myapp.helloworld.grpc.GreeterService;
  21. import example.myapp.helloworld.grpc.GreeterServiceHandlerFactory;
  22. import static akka.http.javadsl.server.Directives.*;
  23. class AuthenticatedGreeterServer {
  24. public static void main(String[] args) throws Exception {
  25. // important to enable HTTP/2 in ActorSystem's config
  26. Config conf = ConfigFactory.parseString("akka.http.server.preview.enable-http2 = on")
  27. .withFallback(ConfigFactory.defaultApplication());
  28. // Akka ActorSystem Boot
  29. ActorSystem sys = ActorSystem.create("HelloWorld", conf);
  30. run(sys).thenAccept(binding -> {
  31. System.out.println("gRPC server bound to: " + binding.localAddress());
  32. });
  33. // ActorSystem threads will keep the app alive until `system.terminate()` is called
  34. }
  35. public static CompletionStage<ServerBinding> run(ActorSystem sys) throws Exception {
  36. Materializer mat = ActorMaterializer.create(sys);
  37. //#http-route
  38. // A Route to authenticate with
  39. Route authentication = path("login", () ->
  40. get(() ->
  41. complete("Psst, please use token XYZ!")
  42. )
  43. );
  44. //#http-route
  45. //#grpc-route
  46. // Instantiate implementation
  47. GreeterService impl = new GreeterServiceImpl(mat);
  48. Function<HttpRequest, CompletionStage<HttpResponse>> handler = GreeterServiceHandlerFactory.create(impl, sys);
  49. // As a Route
  50. Route handlerRoute = RouteUtils.fromFunction(handler, sys);
  51. //#grpc-route
  52. //#grpc-protected
  53. // Protect the handler route
  54. Route protectedHandler =
  55. headerValueByName("token", token -> {
  56. if ("XYZ".equals(token)) {
  57. return handlerRoute;
  58. } else {
  59. return complete(StatusCodes.UNAUTHORIZED);
  60. }
  61. });
  62. //#grpc-protected
  63. //#combined
  64. Route finalRoute = concat(
  65. authentication,
  66. protectedHandler
  67. );
  68. return Http.get(sys).bindAndHandleAsync(
  69. RouteUtils.toFunction(finalRoute, sys),
  70. ConnectHttp.toHost("127.0.0.1", 8090),
  71. mat);
  72. //#combined
  73. }
  74. }