-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVertxModule.java
More file actions
144 lines (120 loc) · 6.2 KB
/
Copy pathVertxModule.java
File metadata and controls
144 lines (120 loc) · 6.2 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
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
package net.bons.comptes.integration;
/* Licence Public Barmic
* copyright 2014-2016 Michel Barret <michel.barret@gmail.com>
*/
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.mail.MailConfig;
import io.vertx.rxjava.core.Vertx;
import io.vertx.rxjava.core.eventbus.EventBus;
import io.vertx.rxjava.ext.mail.MailClient;
import io.vertx.rxjava.ext.mongo.MongoClient;
import io.vertx.rxjava.ext.web.Router;
import io.vertx.rxjava.ext.web.handler.BodyHandler;
import io.vertx.rxjava.ext.web.handler.StaticHandler;
import io.vertx.serviceproxy.ProxyHelper;
import net.bons.comptes.cqrs.*;
import net.bons.comptes.service.EventStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Singleton;
import javax.validation.Validation;
import javax.validation.Validator;
public class VertxModule extends AbstractModule {
private static final Logger LOG = LoggerFactory.getLogger(VertxModule.class);
private final Vertx vertx;
private final JsonObject config;
public VertxModule(Vertx vertx, JsonObject config) {
this.vertx = vertx;
this.config = config;
}
@Override
protected void configure() {
bind(EventBus.class).toInstance(vertx.eventBus());
io.vertx.core.Vertx delegate = (io.vertx.core.Vertx) vertx.getDelegate();
bind(io.vertx.core.Vertx.class).toInstance(delegate);
bind(Vertx.class).toInstance(vertx);
bind(JsonObject.class).toInstance(config);
EventStore service = ProxyHelper.createProxy(EventStore.class, delegate, "database-service-address");
bind(EventStore.class).toInstance(service);
}
@Provides
Router provideRouter(Vertx vertx, StaticHandler staticHandler, GetProject getProject,
ContrubutionUpdateHandler contrubutionUpdateHandler, CreateProjectHandler createProjectHandler,
ContributionHandler contributionHandler, UpdateProject updateProject,
DeleteContribution deleteContribution, PayedContribution payedContribution, Remind remind,
ListProject listProject, DeleteProject deleteProject, OutgoingHandler outgoingHandler) {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
// command
router.post("/api/project").handler(createProjectHandler);
router.post("/api/project/:projectId/contribution").handler(contributionHandler);
router.post("/api/project/:projectId/contribution/:contributionId").handler(
contrubutionUpdateHandler); // update contribution
router.delete("/api/project/:projectId/contribution/:contributionId").handler(deleteContribution);
router.post("/api/project/:projectId/contribution/:contributionId/payed").handler(payedContribution);
router.post("/api/project/:projectId/contribution/:contributionId/remind").handler(remind);
String jsonContentType = "application/json";
router.post("/api/project/:projectId/admin/:adminPass").produces(jsonContentType).handler(updateProject);
router.post("/api/project/:projectId/admin/:adminPass/outgoing/del").produces(jsonContentType).handler(outgoingHandler);
router.post("/api/project/:projectId/admin/:adminPass/outgoing").produces(jsonContentType).handler(outgoingHandler);
if (config.containsKey("root_secret")) {
router.get("/api/admin/" + config.getString("root_secret") + "/project").handler(listProject);
router.delete("/api/admin/" + config.getString("root_secret") + "/project/:projectId").handler(
deleteProject);
}
// query
router.get("/api/project/:projectId").produces(jsonContentType).handler(getProject);
router.get("/api/project/:projectId/admin/:adminPass").produces(jsonContentType).handler(getProject);
router.get("/api/project/:projectId/contribution/:contributionId").produces(jsonContentType).handler(getProject);
router.get("/*").handler(staticHandler);
return router;
}
@Provides
StaticHandler provideStaticHandler() {
StaticHandler staticHandler = StaticHandler.create("public");
staticHandler.setIndexPage("index.html");
return staticHandler;
}
@Provides
MongoConfig provideMongoConfig() {
return new MongoConfig(config.getJsonObject("mongo").getString("collection"));
}
@Provides
@Singleton
MongoClient provideMongoClient(Vertx vertx) {
JsonObject mongo = config.getJsonObject("mongo");
JsonObject host = new JsonObject()
.put("host", mongo.getString("host"))
.put("port", mongo.getInteger("port"));
JsonObject config = new JsonObject()
.put("db_name", mongo.getString("dbname"))
.put("useObjectId", true)
.put("hosts", new JsonArray().add(host))
.put("username", System.getenv("MONGO_USER"))
.put("password", System.getenv("MONGO_PASSWD"));
MongoClient mongoClient = MongoClient.createShared(vertx, config);
mongoClient.createCollectionObservable(mongo.getString("collection"))
.subscribe(aVoid -> LOG.info("Created ok!"),
throwable -> LOG.warn("Can't create the collection {}", throwable.getMessage()));
return mongoClient;
}
@Provides
@Singleton
MailClient provideMailClient() {
JsonObject mailConfig = this.config.getJsonObject("mail");
MailConfig config = new MailConfig().setHostname(mailConfig.getString("host"))
.setSsl(mailConfig.getBoolean("use_ssl", true))
.setPort(mailConfig.getInteger("port", 587))
.setUsername(mailConfig.getString("user"))
.setPassword(mailConfig.getString("password"));
return MailClient.createShared(vertx, config);
}
@Provides
@Singleton
Validator provideValidator() {
return Validation.buildDefaultValidatorFactory().getValidator();
}
}