Move HttpClient/RedisClient from APIv3.java to their relevant util classes
This commit is contained in:
parent
23b9d81923
commit
43b59dcf2c
@ -21,15 +21,17 @@ import com.mongodb.connection.ClusterSettings;
|
||||
import fr.javatic.mongo.jacksonCodec.JacksonCodecProvider;
|
||||
import fr.javatic.mongo.jacksonCodec.ObjectMapperFactory;
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.http.*;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpHeaders;
|
||||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.BodyHandler;
|
||||
import io.vertx.ext.web.handler.LoggerFormat;
|
||||
import io.vertx.ext.web.handler.LoggerHandler;
|
||||
import io.vertx.ext.web.handler.TimeoutHandler;
|
||||
import io.vertx.redis.RedisClient;
|
||||
import io.vertx.redis.RedisOptions;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.frozenorb.apiv3.handler.ActorAttributeHandler;
|
||||
@ -92,11 +94,9 @@ import java.util.concurrent.TimeUnit;
|
||||
@Slf4j
|
||||
public final class APIv3 extends AbstractVerticle {
|
||||
|
||||
@Getter private static HttpClient httpClient;
|
||||
@Getter private static HttpClient httpsClient;
|
||||
@Getter private static Vertx vertxInstance;
|
||||
@Getter private static MongoDatabase database;
|
||||
@Getter private static Properties config = new Properties();
|
||||
@Getter private static RedisClient redisClient;
|
||||
private static final Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Instant.class, new InstantTypeAdapter())
|
||||
.setExclusionStrategies(new FollowAnnotationExclusionStrategy())
|
||||
@ -104,12 +104,11 @@ public final class APIv3 extends AbstractVerticle {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
vertxInstance = vertx;
|
||||
setupConfig();
|
||||
setupDatabase();
|
||||
setupRedis();
|
||||
setupBugsnag();
|
||||
setupHttpServer();
|
||||
setupHttpClient();
|
||||
|
||||
/*V2Importer converter = new V2Importer("mongodb://158.69.126.126", "minehq");
|
||||
|
||||
@ -211,15 +210,6 @@ public final class APIv3 extends AbstractVerticle {
|
||||
return mongoJacksonMapper;
|
||||
}
|
||||
|
||||
private void setupRedis() {
|
||||
redisClient = RedisClient.create(
|
||||
vertx,
|
||||
new RedisOptions()
|
||||
.setAddress(config.getProperty("redis.address"))
|
||||
.setPort(Integer.parseInt(config.getProperty("redis.port")))
|
||||
);
|
||||
}
|
||||
|
||||
private void setupBugsnag() {
|
||||
Client bugsnag = new Client(config.getProperty("bugsnag.apiKey"));
|
||||
bugsnag.setReleaseStage(config.getProperty("general.releaseStage"));
|
||||
@ -227,11 +217,10 @@ public final class APIv3 extends AbstractVerticle {
|
||||
bugsnag.setLogger(new BugsnagSlf4jLogger());
|
||||
}
|
||||
|
||||
// TODO: blockingHandler -> handler
|
||||
private void setupHttpServer() {
|
||||
HttpServer webServer = vertx.createHttpServer(
|
||||
new HttpServerOptions()
|
||||
//.setSsl(true)
|
||||
//.setSsl(true) // TODO
|
||||
.setCompressionSupported(true)
|
||||
);
|
||||
|
||||
@ -264,9 +253,9 @@ public final class APIv3 extends AbstractVerticle {
|
||||
http.post("/ipBans").blockingHandler(new POSTIpBans(), false);
|
||||
http.delete("/ipBans/:id").blockingHandler(new DELETEIpBan(), false);
|
||||
|
||||
http.get("/ipIntel").handler(new GETIpIntel());
|
||||
http.get("/ipIntel/:id").handler(new GETIpIntel());
|
||||
|
||||
http.get("/ipLog").handler(new GETIpLog());
|
||||
http.get("/ipLog/:id").handler(new GETIpLog());
|
||||
|
||||
http.get("/notificationTemplates/:id").handler(new GETNotificationTemplatesId());
|
||||
http.get("/notificationTemplates").handler(new GETNotificationTemplates());
|
||||
@ -320,15 +309,6 @@ public final class APIv3 extends AbstractVerticle {
|
||||
webServer.requestHandler(http::accept).listen(port);
|
||||
}
|
||||
|
||||
private void setupHttpClient() {
|
||||
httpClient = vertx.createHttpClient();
|
||||
httpsClient = vertx.createHttpClient(
|
||||
new HttpClientOptions()
|
||||
.setSsl(true)
|
||||
.setTrustAll(true)
|
||||
);
|
||||
}
|
||||
|
||||
public static void respondJson(RoutingContext ctx, Object response) {
|
||||
respondJson(ctx, 200, response);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package net.frozenorb.apiv3.unsorted;
|
||||
|
||||
import com.google.common.net.MediaType;
|
||||
import com.mongodb.async.SingleResultCallback;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpHeaders;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
@ -12,6 +13,7 @@ import java.util.Map;
|
||||
|
||||
public final class Notification {
|
||||
|
||||
private static final HttpClient httpClient = APIv3.getVertxInstance().createHttpClient();
|
||||
private final String subject;
|
||||
private final String body;
|
||||
|
||||
@ -38,7 +40,7 @@ public final class Notification {
|
||||
.put("key", APIv3.getConfig().getProperty("mandrill.apiKey"))
|
||||
.put("message", message);
|
||||
|
||||
APIv3.getHttpClient().post("mandrillapp.com", "/api/1.0/messages/send.json", (response) -> {
|
||||
httpClient.post("mandrillapp.com", "/api/1.0/messages/send.json", (response) -> {
|
||||
response.bodyHandler((resultBody) -> {
|
||||
callback.onResult(null, null);
|
||||
});
|
||||
|
@ -2,6 +2,8 @@ package net.frozenorb.apiv3.util;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.mongodb.async.SingleResultCallback;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpClientOptions;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import net.frozenorb.apiv3.APIv3;
|
||||
@ -14,10 +16,11 @@ public class MaxMindUtils {
|
||||
|
||||
private static final String maxMindUserId = APIv3.getConfig().getProperty("maxMind.userId");
|
||||
private static final String maxMindLicenseKey = APIv3.getConfig().getProperty("maxMind.maxMindLicenseKey");
|
||||
private static final HttpClient httpsClient = APIv3.getVertxInstance().createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true));
|
||||
|
||||
public static void getInsights(String ip, SingleResultCallback<MaxMindResult> callback) {
|
||||
// We have to specifically use getHttpSClient(), vertx's http client is dumb.
|
||||
APIv3.getHttpsClient().get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> {
|
||||
httpsClient.get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> {
|
||||
response.bodyHandler((body) -> {
|
||||
JsonObject bodyJson = new JsonObject(body.toString());
|
||||
callback.onResult(new MaxMindResult(bodyJson), null);
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.frozenorb.apiv3.util;
|
||||
|
||||
import com.mongodb.async.SingleResultCallback;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.json.DecodeException;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import lombok.experimental.UtilityClass;
|
||||
@ -12,8 +13,10 @@ import java.util.UUID;
|
||||
@UtilityClass
|
||||
public class MojangUtils {
|
||||
|
||||
private static final HttpClient httpClient = APIv3.getVertxInstance().createHttpClient();
|
||||
|
||||
public static void getName(UUID id, SingleResultCallback<String> callback) {
|
||||
APIv3.getHttpClient().get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> {
|
||||
httpClient.get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> {
|
||||
response.bodyHandler((body) -> {
|
||||
try {
|
||||
JsonObject bodyJson = new JsonObject(body.toString());
|
||||
|
@ -5,6 +5,8 @@ import com.warrenstrange.googleauth.GoogleAuthenticator;
|
||||
import com.warrenstrange.googleauth.GoogleAuthenticatorConfig;
|
||||
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
|
||||
import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator;
|
||||
import io.vertx.redis.RedisClient;
|
||||
import io.vertx.redis.RedisOptions;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import net.frozenorb.apiv3.APIv3;
|
||||
import net.frozenorb.apiv3.model.User;
|
||||
@ -14,7 +16,12 @@ import java.util.concurrent.TimeUnit;
|
||||
@UtilityClass
|
||||
public class TotpUtils {
|
||||
|
||||
private static GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator(new GoogleAuthenticatorConfig.GoogleAuthenticatorConfigBuilder().setWindowSize(10).build());
|
||||
private static final GoogleAuthenticator googleAuthenticator = new GoogleAuthenticator(new GoogleAuthenticatorConfig.GoogleAuthenticatorConfigBuilder().setWindowSize(10).build());
|
||||
private static final RedisClient redisClient = RedisClient.create(APIv3.getVertxInstance(),
|
||||
new RedisOptions()
|
||||
.setAddress(APIv3.getConfig().getProperty("redis.address"))
|
||||
.setPort(Integer.parseInt(APIv3.getConfig().getProperty("redis.port")))
|
||||
);
|
||||
|
||||
public static GoogleAuthenticatorKey generateTotpSecret() {
|
||||
return googleAuthenticator.createCredentials();
|
||||
@ -33,7 +40,7 @@ public class TotpUtils {
|
||||
}
|
||||
|
||||
public static void isPreAuthorized(User user, String ip, SingleResultCallback<Boolean> callback) {
|
||||
APIv3.getRedisClient().exists(user.getId() + ":preAuthorizedIp:" + ip.toLowerCase(), (result) -> {
|
||||
redisClient.exists(user.getId() + ":preAuthorizedIp:" + ip.toLowerCase(), (result) -> {
|
||||
if (result.succeeded()) {
|
||||
callback.onResult(result.result() == 1 , null);
|
||||
} else {
|
||||
@ -45,9 +52,9 @@ public class TotpUtils {
|
||||
public static void markPreAuthorized(User user, String ip, long duration, TimeUnit unit, SingleResultCallback<Void> callback) {
|
||||
String key = user.getId() + ":preAuthorizedIp:" + ip.toLowerCase();
|
||||
|
||||
APIv3.getRedisClient().set(key, "", (result) -> {
|
||||
redisClient.set(key, "", (result) -> {
|
||||
if (result.succeeded()) {
|
||||
APIv3.getRedisClient().expire(key, (int) unit.toSeconds(duration), (result2) -> {
|
||||
redisClient.expire(key, (int) unit.toSeconds(duration), (result2) -> {
|
||||
if (result2.succeeded()) {
|
||||
callback.onResult(null, null);
|
||||
} else {
|
||||
@ -61,7 +68,7 @@ public class TotpUtils {
|
||||
}
|
||||
|
||||
public static void wasRecentlyUsed(User user, int code, SingleResultCallback<Boolean> callback) {
|
||||
APIv3.getRedisClient().exists(user.getId() + ":recentTOTPCodes:" + code, (result) -> {
|
||||
redisClient.exists(user.getId() + ":recentTOTPCodes:" + code, (result) -> {
|
||||
if (result.succeeded()) {
|
||||
callback.onResult(result.result() == 1 , null);
|
||||
} else {
|
||||
@ -73,9 +80,9 @@ public class TotpUtils {
|
||||
public static void markRecentlyUsed(User user, int code, SingleResultCallback<Void> callback) {
|
||||
String key = user.getId() + ":recentTOTPCodes:" + code;
|
||||
|
||||
APIv3.getRedisClient().set(key, "", (result) -> {
|
||||
redisClient.set(key, "", (result) -> {
|
||||
if (result.succeeded()) {
|
||||
APIv3.getRedisClient().expire(key, (int) TimeUnit.MINUTES.toSeconds(5), (result2) -> {
|
||||
redisClient.expire(key, (int) TimeUnit.MINUTES.toSeconds(5), (result2) -> {
|
||||
if (result2.succeeded()) {
|
||||
callback.onResult(null, null);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user