in pekko-sample-persistence-dc-java/src/main/java/sample/persistence/res/counter/ThumbsUpHttp.java [37:65]
private Route createRoute(ReplicaId selfReplica, ReplicatedSharding<ThumbsUpCounter.Command> replicatedSharding) {
Duration timeout = Duration.ofSeconds(10);
return
pathPrefix("thumbs-up", () ->
concat(
// example: curl http://0.0.0.0:27345/thumbs-up/pekko
get(() -> path(segment(), resourceId -> {
return onComplete(replicatedSharding.getEntityRefsFor(resourceId).get(selfReplica).<ThumbsUpCounter.State>ask(replyTo -> new ThumbsUpCounter.GetUsers(resourceId, replyTo), timeout), state -> {
Source<ByteString, NotUsed> s =
Source.fromIterator(() -> (state.get()).users.iterator())
.intersperse("\n")
.map(ByteString::fromString);
return complete(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, s));
});
})),
// example: curl -X POST http://0.0.0.0:27345/thumbs-up/pekko/u1
post(() -> {
return path(segments(2), seg -> {
final String resourceId = seg.get(0);
final String userId = seg.get(1);
return onComplete(replicatedSharding.getEntityRefsFor(resourceId).get(selfReplica).<Long>ask(replyTo -> new ThumbsUpCounter.GiveThumbsUp(resourceId, userId, replyTo), timeout), cnt -> {
return complete(cnt.get().toString());
});
});
})
)
);
}