|
| 1 | +package com.pharosproduction.users.api_mobile; |
| 2 | + |
| 3 | +import com.pharosproduction.users.rocksworker.RocksService; |
| 4 | +import io.vertx.core.AbstractVerticle; |
| 5 | +import io.vertx.core.AsyncResult; |
| 6 | +import io.vertx.core.http.HttpServer; |
| 7 | +import io.vertx.core.http.HttpServerOptions; |
| 8 | +import io.vertx.core.http.HttpServerRequest; |
| 9 | +import io.vertx.core.http.HttpServerResponse; |
| 10 | +import io.vertx.core.json.Json; |
| 11 | +import io.vertx.core.json.JsonObject; |
| 12 | +import io.vertx.serviceproxy.ServiceProxyBuilder; |
| 13 | + |
| 14 | +import static com.pharosproduction.users.rocksworker.RocksService.ROCKS_SERVICE_ADDR; |
| 15 | + |
| 16 | +public class ServerVerticle extends AbstractVerticle { |
| 17 | + |
| 18 | + // Variables |
| 19 | + |
| 20 | + private RocksService mRocksService; |
| 21 | + |
| 22 | + // Overrides |
| 23 | + |
| 24 | + @Override |
| 25 | + public void start() throws Exception { |
| 26 | + super.start(); |
| 27 | + |
| 28 | + createServer(); |
| 29 | + |
| 30 | + mRocksService = new ServiceProxyBuilder(vertx) |
| 31 | + .setAddress(ROCKS_SERVICE_ADDR) |
| 32 | + .build(RocksService.class); |
| 33 | + } |
| 34 | + |
| 35 | + // Private |
| 36 | + |
| 37 | + private void createServer() { |
| 38 | + Config config = new Config(config()); |
| 39 | + HttpServerOptions options = config.getHttpOptions(); |
| 40 | + int port = config.getHttpOptions().getPort(); |
| 41 | + |
| 42 | + vertx.createHttpServer(options) |
| 43 | + .requestHandler(this::handleRequest) |
| 44 | + .listen(port, this::handleListener); |
| 45 | + } |
| 46 | + |
| 47 | + private void handleListener(AsyncResult<HttpServer> ar) { |
| 48 | + String res = ar.succeeded() ? "Server started" : "Cannot start server: " + ar.cause(); |
| 49 | + System.out.println(res); |
| 50 | + } |
| 51 | + |
| 52 | + private void handleRequest(HttpServerRequest request) { |
| 53 | + HttpServerResponse response = request.response() |
| 54 | + .putHeader("content-type", "application/json"); |
| 55 | + |
| 56 | + switch (request.method()) { |
| 57 | + case GET: userGet(response); break; |
| 58 | + case POST: userCreate(request, response); break; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void userGet(HttpServerResponse response) { |
| 63 | + mRocksService.getUser(ar -> userGetResponse(ar, response)); |
| 64 | + } |
| 65 | + |
| 66 | + private void userGetResponse(AsyncResult<JsonObject> ar, HttpServerResponse response) { |
| 67 | + if (ar.succeeded()) { |
| 68 | + JsonObject json = ar.result(); |
| 69 | + |
| 70 | + response.setStatusCode(200) |
| 71 | + .end(json.toString()); |
| 72 | + } else { |
| 73 | + response.setStatusCode(404) |
| 74 | + .end(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void userCreate(HttpServerRequest request, HttpServerResponse response) { |
| 79 | + String userId = request.params() |
| 80 | + .get("userId"); |
| 81 | + mRocksService.createUser(userId, ar -> userCreateResponse(ar, response)); |
| 82 | + } |
| 83 | + |
| 84 | + private void userCreateResponse(AsyncResult<JsonObject> ar, HttpServerResponse response) { |
| 85 | + if (ar.succeeded()) { |
| 86 | + JsonObject json = ar.result(); |
| 87 | + |
| 88 | + response.setStatusCode(201) |
| 89 | + .end(json.toString()); |
| 90 | + } else { |
| 91 | + response.setStatusCode(422) |
| 92 | + .end(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments