/docs/src/main/paradox/server/akka-http.md

https://github.com/akka/akka-grpc · Markdown · 67 lines · 44 code · 23 blank · 0 comment · 0 complexity · f5910897a6c79406a480e3c0d3e21e80 MD5 · raw file

  1. # Akka HTTP interop
  2. Akka gRPC is built on top of [Akka HTTP](https://docs.akka.io/docs/akka-http).
  3. This means it is possible to leverage the Akka HTTP API's to create more
  4. complicated services, for example serving non-gRPC endpoints next to
  5. gRPC endpoints or adding additional behavior around your gRPC routes.
  6. ## Example: authentication/authorization
  7. One use case could be adding cross-cutting concerns such as
  8. authentication/authorization. Suppose you have an API that
  9. you want to secure using a token. You already have a regular
  10. HTTP API that users can use to obtain a token, and want to
  11. secure your gRPC routes to only accept calls that include this
  12. token.
  13. ### Akka HTTP authentication route
  14. This route could be any arbitrary Akka HTTP route. For this example
  15. we just provide a hint in the response body:
  16. Scala
  17. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-scala/src/main/scala/example/myapp/helloworld/AuthenticatedGreeterServer.scala) { #http-route }
  18. Java
  19. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-java/src/main/java/example/myapp/helloworld/AuthenticatedGreeterServer.java) { #http-route }
  20. ### Akka gRPC route
  21. We create the Akka gRPC service implementation, and convert it to a @apidoc[Route$] as well:
  22. Scala
  23. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-scala/src/main/scala/example/myapp/helloworld/AuthenticatedGreeterServer.scala) { #grpc-route }
  24. Java
  25. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-java/src/main/java/example/myapp/helloworld/AuthenticatedGreeterServer.java) { #grpc-route }
  26. ### Securing the Akka gRPC route
  27. We can wrap the gRPC route just like any @apidoc[Route$], applying the authorization:
  28. Scala
  29. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-scala/src/main/scala/example/myapp/helloworld/AuthenticatedGreeterServer.scala) { #grpc-protected }
  30. Java
  31. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-java/src/main/java/example/myapp/helloworld/AuthenticatedGreeterServer.java) { #grpc-protected }
  32. ### Tying it all together
  33. Finally we can combine the routes and serve them. Remember we need to use `bindAndHandleAsync` to enable HTTP/2 support:
  34. Scala
  35. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-scala/src/main/scala/example/myapp/helloworld/AuthenticatedGreeterServer.scala) { #combined }
  36. Java
  37. : @@snip [AuthenticatedGreeterServer.java](/plugin-tester-java/src/main/java/example/myapp/helloworld/AuthenticatedGreeterServer.java) { #combined }
  38. ### Future work
  39. For more in-depth integration you might want to pass information from the
  40. Akka HTTP route into your gRPC service implementation.
  41. Currently, you could achieve this by adding the required information to your
  42. service implementation constructor, and constructing a new Handler for each request.
  43. In the future we plan to provide a nicer API for this, for example we could pass the
  44. Akka HTTP attributes (introduced in 10.2.0) as Metadata when using the PowerApi.