-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertxHandler.groovy
81 lines (50 loc) · 2.12 KB
/
VertxHandler.groovy
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
// VertxHandler.groovy
package com.serverless
import com.amazonaws.services.lambda.runtime.*
import io.vertx.core.*
import java.util.concurrent.*
class VertxHandler implements RequestHandler<Map, Map> {
// Initialize Vertx instance and deploy UserService Verticle
final Vertx vertxInstance = {
System.setProperty('vertx.disableFileCPResolving', 'true')
final vertx = Vertx.vertx()
vertx.deployVerticle(UserService.newInstance())
return vertx
}()
@Override Map handleRequest(Map input, Context context) {
final future = new CompletableFuture<Map>()
// Send message to event bus using httpmethod:resource as dynamic channel final eventBusAddress = "${input.httpMethod}:${input.resource}" vertxInstance.eventBus().send(eventBusAddress, input, { asyncResult ->
if (asyncResult.succeeded()) {
future.complete(asyncResult.result().body())
} else {
future.completeExceptionally(asyncResult.cause()) }
})
future.get(5, TimeUnit.SECONDS)
}
class UserService extends AbstractVerticle {
@Override
void start() throws Exception {
final eventBus = vertx.eventBus()
eventBus.consumer('GET:/users') { message ->
// Do something with Vert.x async, reactive APIs
message.reply([statusCode: 200, body: 'Received GET:/users'])
}
eventBus.consumer('POST:/users') { message ->
// Do something with Vert.x async, reactive APIs
message.reply([statusCode: 201, body: 'Received POST:/users'])
}
eventBus.consumer('GET:/users/{id}') { message ->
// Do something with Vert.x async, reactive APIs
message.reply([statusCode: 200, body: 'Received GET:/users/{id}'])
}
eventBus.consumer('PUT:/users/{id}') { message ->
// Do something with Vert.x async, reactive APIs
message.reply([statusCode: 200, body: 'Received PUT:/users/{id}'])
}
eventBus.consumer('DELETE:/users/{id}') { message ->
// Do something with Vert.x async, reactive APIs
message.reply([statusCode: 200, body: 'Received DELETE:/users/{id}'])
}
}
}
}