forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
213 lines (171 loc) · 6.05 KB
/
Server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package io.vertx.example.web.angularjs;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.example.util.Runner;
import io.vertx.ext.mongo.MongoClient;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;
import java.util.LinkedList;
import java.util.List;
/*
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
*/
public class Server extends AbstractVerticle {
// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(Server.class);
}
private MongoClient mongo;
@Override
public void start() throws Exception {
// Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
// the load function just populates some data on the storage
loadData(mongo);
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
// define some REST API
router.get("/api/users").handler(ctx -> {
mongo.find("users", new JsonObject(), lookup -> {
// error handling
if (lookup.failed()) {
ctx.fail(500);
return;
}
// now convert the list to a JsonArray because it will be easier to encode the final object as the response.
final JsonArray json = new JsonArray();
for (JsonObject o : lookup.result()) {
json.add(o);
}
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json");
ctx.response().end(json.encode());
});
});
router.get("/api/users/:id").handler(ctx -> {
mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> {
// error handling
if (lookup.failed()) {
ctx.fail(500);
return;
}
JsonObject user = lookup.result();
if (user == null) {
ctx.fail(404);
} else {
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json");
ctx.response().end(user.encode());
}
});
});
router.post("/api/users").handler(ctx -> {
JsonObject newUser = ctx.getBodyAsJson();
mongo.findOne("users", new JsonObject().put("username", newUser.getString("username")), null, lookup -> {
// error handling
if (lookup.failed()) {
ctx.fail(500);
return;
}
JsonObject user = lookup.result();
if (user != null) {
// already exists
ctx.fail(500);
} else {
mongo.insert("users", newUser, insert -> {
// error handling
if (insert.failed()) {
ctx.fail(500);
return;
}
// add the generated id to the user object
newUser.put("_id", insert.result());
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json");
ctx.response().end(newUser.encode());
});
}
});
});
router.put("/api/users/:id").handler(ctx -> {
mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> {
// error handling
if (lookup.failed()) {
ctx.fail(500);
return;
}
JsonObject user = lookup.result();
if (user == null) {
// does not exist
ctx.fail(404);
} else {
// update the user properties
JsonObject update = ctx.getBodyAsJson();
user.put("username", update.getString("username"));
user.put("firstName", update.getString("firstName"));
user.put("lastName", update.getString("lastName"));
user.put("address", update.getString("address"));
mongo.replace("users", new JsonObject().put("_id", ctx.request().getParam("id")), user, replace -> {
// error handling
if (replace.failed()) {
ctx.fail(500);
return;
}
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json");
ctx.response().end(user.encode());
});
}
});
});
router.delete("/api/users/:id").handler(ctx -> {
mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> {
// error handling
if (lookup.failed()) {
ctx.fail(500);
return;
}
JsonObject user = lookup.result();
if (user == null) {
// does not exist
ctx.fail(404);
} else {
mongo.remove("users", new JsonObject().put("_id", ctx.request().getParam("id")), remove -> {
// error handling
if (remove.failed()) {
ctx.fail(500);
return;
}
ctx.response().setStatusCode(204);
ctx.response().end();
});
}
});
});
// Create a router endpoint for the static content.
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
private void loadData(MongoClient db) {
db.dropCollection("users", drop -> {
if (drop.failed()) {
throw new RuntimeException(drop.cause());
}
List<JsonObject> users = new LinkedList<>();
users.add(new JsonObject()
.put("username", "pmlopes")
.put("firstName", "Paulo")
.put("lastName", "Lopes")
.put("address", "The Netherlands"));
users.add(new JsonObject()
.put("username", "timfox")
.put("firstName", "Tim")
.put("lastName", "Fox")
.put("address", "The Moon"));
for (JsonObject user : users) {
db.insert("users", user, res -> {
System.out.println("inserted " + user.encode());
});
}
});
}
}