Create core routers

This commit is contained in:
Colin McDonald 2016-03-05 23:37:33 -05:00
parent 1a52326f29
commit 516f831277
9 changed files with 233 additions and 77 deletions

View File

@ -6,16 +6,9 @@ import com.mongodb.async.client.MongoDatabase;
import io.vertx.core.AbstractVerticle; import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router; import io.vertx.ext.web.Router;
import lombok.Getter; import lombok.Getter;
import net.frozenorb.apiv3.accessor.AuditLog; import net.frozenorb.apiv3.routes.*;
import net.frozenorb.apiv3.accessor.Grants;
import net.frozenorb.apiv3.accessor.Servers;
import net.frozenorb.apiv3.accessor.Users;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.JsonUtils;
import net.frozenorb.apiv3.util.MongoUtils; import net.frozenorb.apiv3.util.MongoUtils;
import java.util.UUID;
public final class APIv3 extends AbstractVerticle { public final class APIv3 extends AbstractVerticle {
@Getter private static MongoDatabase mongo; @Getter private static MongoDatabase mongo;
@ -24,83 +17,33 @@ public final class APIv3 extends AbstractVerticle {
// TODO: consistency -- sometimes we refer to a user as user or target. // TODO: consistency -- sometimes we refer to a user as user or target.
public void start() { public void start() {
Router coreHttpRouter = Router.router(vertx); setupDatabase();
setupHttp();
}
public void setupDatabase() {
mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray()); mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray());
}
public void setupHttp() {
Router rootRouter = Router.router(vertx);
// We always reply in JSON. // We always reply in JSON.
coreHttpRouter.route("/*").handler(ctx -> { rootRouter.route("/*").handler(ctx -> {
ctx.response().putHeader("content-type", "application/json"); ctx.response().putHeader("content-type", "application/json");
ctx.next(); ctx.next();
}); });
coreHttpRouter.get("/servers").handler(ctx -> { rootRouter.mountSubRouter("/auditLog", AuditLogRouter.create(vertx));
Servers.findAll((servers, error) -> { rootRouter.mountSubRouter("/grants", GrantRouter.create(vertx));
if (error != null) { rootRouter.mountSubRouter("/ipLog", IPLogRouter.create(vertx));
ctx.response().end(ErrorUtils.createResponse(error).toJson()); rootRouter.mountSubRouter("/notifications", NotificationRouter.create(vertx));
} else { rootRouter.mountSubRouter("/punishments", PunishmentRouter.create(vertx));
ctx.response().end(JsonUtils.toLiteJsonString(servers)); rootRouter.mountSubRouter("/serverGroups", ServerGroupRouter.create(vertx));
} rootRouter.mountSubRouter("/servers", ServerGroupRouter.create(vertx));
}); rootRouter.mountSubRouter("/users", UserRouter.create(vertx));
});
coreHttpRouter.get("/server/:server").handler(ctx -> { vertx.createHttpServer().requestHandler(rootRouter::accept).listen(8080);
String serverId = ctx.request().getParam("server");
Servers.findById(serverId, (server, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else if (server != null) {
ctx.response().end(JsonUtils.toLiteJsonString(server));
} else {
ctx.response().end(ErrorUtils.createResponse("Server '" + serverId + "' not found.").toJson());
}
});
});
coreHttpRouter.get("/user/:user").handler(ctx -> {
UUID target = UUID.fromString(ctx.request().getParam("user"));
Users.findById(target.toString(), (user, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else if (user != null) {
ctx.response().end(JsonUtils.toLiteJsonString(user));
} else {
ctx.response().end(ErrorUtils.createResponse("User '" + target + "' not found.").toJson());
}
});
});
coreHttpRouter.get("/user/:user/grants").handler(ctx -> {
UUID target = UUID.fromString(ctx.request().getParam("user"));
Grants.findByTarget(target, (grants, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(JsonUtils.toLiteJsonString(grants));
}
});
});
coreHttpRouter.get("/auditLog").handler(ctx -> {
AuditLog.findAll((auditLogEntries, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(JsonUtils.toLiteJsonString(auditLogEntries));
}
});
});
coreHttpRouter.get("/doAction/:actionType").handler(ctx -> {
String actionType = ctx.request().getParam("actionType");
//AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document());
ctx.response().end("{'logged': true}");
});
vertx.createHttpServer().requestHandler(coreHttpRouter::accept).listen(8080);
} }
} }

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class AuditLogRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class GrantRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class IPLogRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class NotificationRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class PunishmentRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class ServerGroupRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}

View File

@ -0,0 +1,87 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class ServerRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
/*
coreHttpRouter.get("/servers").handler(ctx -> {
Servers.findAll((servers, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(JsonUtils.toLiteJsonString(servers));
}
});
});
coreHttpRouter.get("/server/:server").handler(ctx -> {
String serverId = ctx.request().getParam("server");
Servers.findById(serverId, (server, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else if (server != null) {
ctx.response().end(JsonUtils.toLiteJsonString(server));
} else {
ctx.response().end(ErrorUtils.createResponse("Server '" + serverId + "' not found.").toJson());
}
});
});
coreHttpRouter.get("/user/:user").handler(ctx -> {
UUID target = UUID.fromString(ctx.request().getParam("user"));
Users.findById(target.toString(), (user, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else if (user != null) {
ctx.response().end(JsonUtils.toLiteJsonString(user));
} else {
ctx.response().end(ErrorUtils.createResponse("User '" + target + "' not found.").toJson());
}
});
});
coreHttpRouter.get("/user/:user/grants").handler(ctx -> {
UUID target = UUID.fromString(ctx.request().getParam("user"));
Grants.findByTarget(target, (grants, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(JsonUtils.toLiteJsonString(grants));
}
});
});
coreHttpRouter.get("/auditLog").handler(ctx -> {
AuditLog.findAll((auditLogEntries, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(JsonUtils.toLiteJsonString(auditLogEntries));
}
});
});
coreHttpRouter.get("/doAction/:actionType").handler(ctx -> {
String actionType = ctx.request().getParam("actionType");
//AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document());
ctx.response().end("{'logged': true}");
});
*/
}

View File

@ -0,0 +1,18 @@
package net.frozenorb.apiv3.routes;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.experimental.UtilityClass;
@UtilityClass
public class UserRouter {
public static Router create(Vertx vertx) {
Router router = Router.router(vertx);
return router;
}
}