-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiVerticle.java
More file actions
102 lines (85 loc) · 3.48 KB
/
ApiVerticle.java
File metadata and controls
102 lines (85 loc) · 3.48 KB
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
package com.example;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import java.time.Instant;
public class ApiVerticle extends VerticleBase {
private Instant startedAt;
@Override
public Future<?> start() {
this.startedAt = Instant.now();
JsonObject cfg = config();
int port = cfg.getInteger("httpPort", 8080);
String name = cfg.getString("name", "unnamed-verticle");
// endpoints: массив объектов
// { "path": "/x", "method": "GET", "response": { "status": .., "headers": {...}, "body": "..." } }
JsonArray endpointsCfg = cfg.getJsonArray("endpoints", new JsonArray());
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
// Динамические endpoints
for (int i = 0; i < endpointsCfg.size(); i++) {
JsonObject ep = endpointsCfg.getJsonObject(i);
String path = ep.getString("path", "/");
String method = ep.getString("method", "GET").toUpperCase();
JsonObject resp = ep.getJsonObject("response", new JsonObject());
int status = resp.getInteger("status", 200);
JsonObject headers = resp.getJsonObject("headers", new JsonObject()
.put("Content-Type", "text/plain"));
String body = resp.getString("body",
"Hello from " + path + " on port " + port + " of " + name);
// простая регистрация по методу
switch (method) {
case "POST":
router.post(path).handler(ctx -> sendConfiguredResponse(ctx, status, headers, body));
break;
case "PUT":
router.put(path).handler(ctx -> sendConfiguredResponse(ctx, status, headers, body));
break;
case "DELETE":
router.delete(path).handler(ctx -> sendConfiguredResponse(ctx, status, headers, body));
break;
default:
router.get(path).handler(ctx -> sendConfiguredResponse(ctx, status, headers, body));
}
}
// /health
router.get("/health").handler(ctx -> {
JsonArray endpointsJson = endpointsCfg.stream()
.map(o -> (JsonObject) o)
.map(o -> new JsonObject()
.put("path", o.getString("path", "/"))
.put("method", o.getString("method", "GET")))
.collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
JsonObject health = new JsonObject()
.put("name", name)
.put("port", port)
.put("startedAt", startedAt.toString())
.put("endpoints", endpointsJson);
ctx.response()
.setStatusCode(200)
.putHeader("Content-Type", "application/json")
.end(health.encode());
});
return vertx.createHttpServer()
.requestHandler(router)
.listen(port).onSuccess( server -> {
System.out.println("API verticle " + name + " on port " + port);
}).onFailure(handle-> {
System.out.println("HTTP/REST Sever fail with: " + handle.getMessage());
});
}
private void sendConfiguredResponse(RoutingContext ctx,
int status,
JsonObject headers,
String body) {
var resp = ctx.response().setStatusCode(status);
for (String key : headers.fieldNames()) {
resp.putHeader(key, headers.getString(key));
}
resp.end(body);
}
}