Skip to content

Commit dccc303

Browse files
committed
make two services best friends
0 parents  commit dccc303

File tree

18 files changed

+829
-0
lines changed

18 files changed

+829
-0
lines changed

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.DS_Store
2+
.gradle
3+
.idea
4+
.classpath
5+
.project
6+
.settings
7+
.yardoc
8+
.yardopts
9+
build
10+
target
11+
out
12+
*.iml
13+
*.ipr
14+
*.iws
15+
.vertx
16+
test-output
17+
src/scratchpad
18+
test-results
19+
test-tmp
20+
*.class
21+
*.swp
22+
.vertx
23+
target

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tutorial Vert.x REST on Java.
2+
3+
## You can find it in our Medium publication
4+
[Pharos Production Medium Article - Vert.x REST on Java](https://medium.com/pharos-production/vert-x-restful-services-on-java-6a4ed8a30489).
5+
6+
Also you're welcome to say hello to us
7+
[Pharos Production - Blockchain and FinTech Software Development](https://pharosproduction.com)

api_mobile/conf/config.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"http": {
3+
"host": "localhost",
4+
"port": 30000
5+
}
6+
}

api_mobile/pom.xml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<!-- Package Info -->
8+
9+
<parent>
10+
<artifactId>users</artifactId>
11+
<groupId>com.pharosproduction</groupId>
12+
<version>1.0</version>
13+
</parent>
14+
15+
<artifactId>api_mobile</artifactId>
16+
17+
<!-- Properties -->
18+
19+
<properties>
20+
<main.verticle>com.pharosproduction.users.api_mobile.ApiMobileVerticle</main.verticle>
21+
</properties>
22+
23+
<!-- Dependencies -->
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.pharosproduction</groupId>
28+
<artifactId>common</artifactId>
29+
<version>1.0</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.pharosproduction</groupId>
34+
<artifactId>rocksworker</artifactId>
35+
<version>1.0</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.pharosproduction.users.api_mobile;
2+
3+
import com.pharosproduction.users.common.MicroserviceVerticle;
4+
import io.vertx.core.AsyncResult;
5+
import io.vertx.core.DeploymentOptions;
6+
import io.vertx.core.json.JsonObject;
7+
8+
public class ApiMobileVerticle extends MicroserviceVerticle {
9+
10+
// Constants
11+
12+
private static final String ENDPOINT = "users";
13+
14+
// Overrides
15+
16+
@Override
17+
public void start() throws Exception {
18+
super.start();
19+
20+
JsonObject configObj = config();
21+
deployServer(configObj);
22+
publishEndpoint(configObj);
23+
}
24+
25+
// Private
26+
27+
private void deployServer(JsonObject configObj) {
28+
String className = ServerVerticle.class.getName();
29+
DeploymentOptions options = new DeploymentOptions()
30+
.setConfig(configObj);
31+
32+
vertx.deployVerticle(className, options);
33+
}
34+
35+
private void publishEndpoint(JsonObject configObj) throws Exception {
36+
Config config = new Config(configObj);
37+
String host = config.getHttpOptions().getHost();
38+
int port = config.getHttpOptions().getPort();
39+
40+
publishHttpEndpoint(ENDPOINT, host, port, this::onEndpointPublished);
41+
}
42+
43+
private void onEndpointPublished(AsyncResult<Void> ar) {
44+
String res = ar.failed() ? ar.cause().getMessage() : "Successfully Published";
45+
System.out.println(ApiMobileVerticle.class.getName() + ": " + res);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.pharosproduction.users.api_mobile;
2+
3+
import io.vertx.core.http.HttpServerOptions;
4+
import io.vertx.core.http.HttpVersion;
5+
import io.vertx.core.json.JsonObject;
6+
7+
import java.util.List;
8+
9+
class Config {
10+
11+
// Constants
12+
13+
private static final String HTTP = "http";
14+
private static final String HOST = "host";
15+
private static final String PORT = "port";
16+
private static final int PORT_DEFAULT = 8080;
17+
18+
// Variables
19+
20+
private final HttpServerOptions mHttpOptions;
21+
22+
// Constructors
23+
24+
Config(JsonObject config) {
25+
JsonObject httpConfig = config.getJsonObject(HTTP);
26+
String host = httpConfig.getString(HOST);
27+
int port = httpConfig.getInteger(PORT, PORT_DEFAULT);
28+
List<HttpVersion> alpns = List.of(HttpVersion.HTTP_1_1, HttpVersion.HTTP_2);
29+
30+
mHttpOptions = new HttpServerOptions();
31+
mHttpOptions.setAlpnVersions(alpns);
32+
mHttpOptions.setHost(host);
33+
mHttpOptions.setPort(port);
34+
}
35+
36+
// Getters
37+
38+
HttpServerOptions getHttpOptions() {
39+
return mHttpOptions;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.pharosproduction.users.api_mobile;
2+
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonAutoDetect
7+
class User {
8+
9+
// Variables
10+
11+
private String mFirstName;
12+
private String mLastName;
13+
14+
// Constructors
15+
16+
User(String firstName, String lastName) {
17+
mFirstName = firstName;
18+
mLastName = lastName;
19+
}
20+
21+
// Accessors
22+
23+
@JsonProperty
24+
String getFirstName() {
25+
return mFirstName;
26+
}
27+
28+
@JsonProperty
29+
String getLastName() {
30+
return mLastName;
31+
}
32+
}
33+

common/pom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<!-- Package Info -->
8+
9+
<parent>
10+
<artifactId>users</artifactId>
11+
<groupId>com.pharosproduction</groupId>
12+
<version>1.0</version>
13+
</parent>
14+
15+
<artifactId>common</artifactId>
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.pharosproduction.users.common;
2+
3+
import io.vertx.core.DeploymentOptions;
4+
import io.vertx.core.VertxOptions;
5+
import io.vertx.core.json.JsonObject;
6+
7+
import java.io.File;
8+
import java.io.FileNotFoundException;
9+
import java.util.Scanner;
10+
11+
public class Launcher extends io.vertx.core.Launcher {
12+
13+
// Main
14+
15+
public static void main(String[] args) {
16+
new Launcher().dispatch(args);
17+
}
18+
19+
// Constants
20+
21+
private static final String configPath = "conf/config.json";
22+
private static final String host = "127.0.0.1";
23+
24+
// Overrides
25+
26+
@Override
27+
public void beforeStartingVertx(VertxOptions options) {
28+
options.setClustered(true)
29+
.setClusterHost(host);
30+
}
31+
32+
@Override
33+
public void beforeDeployingVerticle(DeploymentOptions deploymentOptions) {
34+
super.beforeDeployingVerticle(deploymentOptions);
35+
36+
defaultOptions(deploymentOptions);
37+
38+
try {
39+
readConfig(deploymentOptions);
40+
} catch (FileNotFoundException e) {
41+
System.out.println("Unable to read config: " + e.getMessage());
42+
}
43+
}
44+
45+
// Private
46+
47+
private void defaultOptions(DeploymentOptions options) {
48+
if (options.getConfig() != null) return;
49+
50+
options.setConfig(new JsonObject());
51+
}
52+
53+
private void readConfig(DeploymentOptions options) throws FileNotFoundException {
54+
File configFile = new File(configPath);
55+
JsonObject config = getConfig(configFile);
56+
options.getConfig().mergeIn(config);
57+
}
58+
59+
private JsonObject getConfig(File config) throws FileNotFoundException {
60+
if (!config.isFile()) return new JsonObject();
61+
62+
Scanner scanner = new Scanner(config).useDelimiter("\\A");
63+
String confStr = scanner.next();
64+
65+
return new JsonObject(confStr);
66+
}
67+
}

0 commit comments

Comments
 (0)