in plugin-tester-java/src/main/java/example/myapp/helloworld/AuthenticatedGreeterServer.java [53:96]
public static CompletionStage<ServerBinding> run(ActorSystem sys) throws Exception {
Materializer mat = SystemMaterializer.get(sys).materializer();
//#http-route
// A Route to authenticate with
Route authentication = path("login", () ->
get(() ->
complete("Psst, please use token XYZ!")
)
);
//#http-route
//#grpc-route
// Instantiate implementation
GreeterService impl = new GreeterServiceImpl(mat);
Function<HttpRequest, CompletionStage<HttpResponse>> handler = GreeterServiceHandlerFactory.create(impl, sys);
// As a Route
Route handlerRoute = handle(handler);
//#grpc-route
//#grpc-protected
// Protect the handler route
Route protectedHandler =
headerValueByName("token", token -> {
if ("XYZ".equals(token)) {
return handlerRoute;
} else {
return complete(StatusCodes.UNAUTHORIZED);
}
});
//#grpc-protected
//#combined
Route finalRoute = concat(
authentication,
protectedHandler
);
return Http.get(sys)
.newServerAt("127.0.0.1", 8090)
.bind(finalRoute);
//#combined
}