Advertisement
shakasu

Untitled

Feb 24th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import io.vertx.core.Future;
  2. import io.vertx.core.Promise;
  3. import io.vertx.core.Vertx;
  4. import io.vertx.core.buffer.Buffer;
  5. import io.vertx.core.http.HttpServer;
  6. import io.vertx.core.http.HttpServerResponse;
  7. import io.vertx.core.json.JsonObject;
  8. import io.vertx.core.net.SocketAddress;
  9. import io.vertx.ext.web.Route;
  10. import io.vertx.ext.web.Router;
  11. import io.vertx.ext.web.client.HttpResponse;
  12. import io.vertx.ext.web.client.WebClient;
  13.  
  14. public class Sandbox {
  15.     final private static int PORT_TDG = 8181;
  16.     final private static int PORT_MY = 8080;
  17.     final private static String HOST = "localhost";
  18.     final private static String TOKEN = "9f226286-e0d8-4125-ad43-b123b4e464da";
  19.     final private static String TYPE = "application/json;charset=utf-8";
  20.  
  21.     public static void main(String[] args) {
  22.         Vertx vertx = Vertx.vertx();
  23.         WebClient client = WebClient.create(vertx);
  24.         Router router = Router.router(vertx);
  25.         Promise<HttpResponse<Buffer>> promise = Promise.promise();
  26.  
  27.         JsonObject request = new JsonObject()
  28.                 .put("query", "{Book { uuid, book_name, author }}");
  29.  
  30.         router.post("/graphql")
  31.                 .handler(ctx -> {
  32.                     client
  33.                             .post("/graphql")
  34.                             .sendJson(request, t -> {
  35.                                 if (t.succeeded()) {
  36.                                     promise.complete(t.result());
  37.                                 } else {
  38.                                     promise.fail(new Exception("Error while sending request to tarantool", t.cause()));
  39.                                 }
  40.                             });
  41.                 });
  42.  
  43.         promise.future().onComplete(t -> {
  44.                     if (t.succeeded()) {
  45.                         var result = t.result();
  46.                         var response = ctx.response()
  47.                                 .setStatusCode(result.statusCode())
  48.                                 .setChunked(true);
  49.                         copyHeaders(result, response);
  50.                         response.write(result.bodyAsBuffer())
  51.                                 .end();
  52.                     }
  53.                 });
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement