use of com.szmirren.vxApi.core.options.VxApisDTO in project VX-API-Gateway by EliMirren.
the class ClientVerticle method updtAPI.
/**
* 更新一个API
*
* @param rct
*/
public void updtAPI(RoutingContext rct) {
User user = rct.user();
HttpServerResponse response = rct.response().putHeader(CONTENT_TYPE, CONTENT_VALUE_JSON_UTF8);
user.isAuthorized(VxApiRolesConstant.WRITE, res -> {
if (res.succeeded()) {
if (res.result()) {
LOG.info(MessageFormat.format("[user : {0}] 执行修改应用...", rct.session().<String>get("userName")));
VxApisDTO dto = VxApisDTO.fromJson(rct.getBodyAsJson());
if (dto.getApiCreateTime() == null) {
dto.setApiCreateTime(Instant.now());
}
JsonObject param = new JsonObject();
param.put("apiName", dto.getApiName());
param.put("api", dto.toJson());
vertx.eventBus().<Integer>send(thisVertxName + VxApiEventBusAddressConstant.UPDT_API, param, cres -> {
if (cres.succeeded()) {
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, cres.result().body()));
LOG.info(MessageFormat.format("[user : {0}] 执行修改API:{2}-->结果: {1}", rct.session().<String>get("userName"), cres.result().body(), dto.getApiName()));
} else {
LOG.error(MessageFormat.format("[user : {0}] 执行修改API-->失败:{1}", rct.session().get("userName"), cres.cause()));
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, cres.cause().toString()));
}
});
} else {
LOG.error(MessageFormat.format("[user : {0}] 执行修改API-->失败:未授权或者无权利", rct.session().get("userName")));
response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C401));
}
} else {
LOG.error(MessageFormat.format("[user : {0}] 执行修改API-->失败:{1}", rct.session().get("userName"), res.cause()));
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, res.cause().getMessage()));
}
});
}
use of com.szmirren.vxApi.core.options.VxApisDTO in project VX-API-Gateway by EliMirren.
the class ClientVerticle method addAPI.
/**
* 添加一个API
*
* @param rct
*/
public void addAPI(RoutingContext rct) {
User user = rct.user();
HttpServerResponse response = rct.response().putHeader(CONTENT_TYPE, CONTENT_VALUE_JSON_UTF8);
user.isAuthorized(VxApiRolesConstant.WRITE, res -> {
if (res.succeeded()) {
if (res.result()) {
LOG.info(MessageFormat.format("[user : {0}] 执行添加API...", rct.session().<String>get("userName")));
JsonObject bodyAsJson = rct.getBodyAsJson();
VxApisDTO dto = VxApisDTO.fromJson(bodyAsJson);
dto.setApiCreateTime(Instant.now());
JsonObject param = new JsonObject();
param.put("apiName", dto.getApiName());
param.put("appName", dto.getAppName());
param.put("api", dto.toJson());
vertx.eventBus().<Integer>send(thisVertxName + VxApiEventBusAddressConstant.ADD_API, param, cres -> {
if (cres.succeeded()) {
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, cres.result().body()));
LOG.info(MessageFormat.format("[user : {0}] 执行添加API-->结果: {1}", rct.session().<String>get("userName"), cres.result().body()));
} else {
LOG.error(MessageFormat.format("[user : {0}] 执行添加API-->失败:{1}", rct.session().get("userName"), cres.cause()));
if (cres.cause().toString().contains("UNIQUE")) {
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C1444, cres.cause().toString()));
} else {
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, cres.cause().toString()));
}
}
});
} else {
LOG.error(MessageFormat.format("[user : {0}] 执行添加API-->失败:未授权或者无权利", rct.session().get("userName")));
response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C401));
}
} else {
LOG.error(MessageFormat.format("[user : {0}] 执行添加API-->失败:{1}", rct.session().get("userName"), res.cause()));
response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, res.cause().getMessage()));
}
});
}
use of com.szmirren.vxApi.core.options.VxApisDTO in project VX-API-Gateway by EliMirren.
the class VxApiApplication method addRoute.
/**
* 添加一个路由
*
* @param msg
*/
public void addRoute(Message<JsonObject> msg) {
JsonObject body = msg.body().getJsonObject("api");
VxApisDTO dto = VxApisDTO.fromJson(body);
if (dto != null) {
VxApis api = new VxApis(dto);
// 是否代理启动API到当前应用
boolean otherRouteAdd = msg.body().getBoolean("elseRouteToThis", false);
if (otherRouteAdd) {
// 服务器的类型1=http,2=https,3=webSocket
int type = msg.body().getInteger("serverType", 0);
if (type == 1) {
addHttpRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else if (type == 2) {
addHttpsRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
res.cause().printStackTrace();
}
});
} else {
msg.fail(500, "不存在的服务");
}
} else {
// 本应用添加API,既属于自己的网关应用添加API
if (httpRouter != null && httpsRouter != null) {
Future<Boolean> httpFuture = Future.future(http -> addHttpRouter(api, http));
Future<Boolean> httpsFuture = Future.<Boolean>future(https -> addHttpsRouter(api, https));
CompositeFuture.all(httpFuture, httpsFuture).setHandler(res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else if (httpRouter != null) {
addHttpRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else if (httpsRouter != null) {
addHttpsRouter(api, res -> {
if (res.succeeded()) {
msg.reply(1);
} else {
msg.fail(500, res.cause().getMessage());
}
});
} else {
msg.fail(404, "找不到的服务器可以加载API");
}
}
} else {
msg.fail(1400, "API参数不能为null,请检查APIDTO需要实例化的JSON编写是否正确");
}
}
use of com.szmirren.vxApi.core.options.VxApisDTO in project VX-API-Gateway by EliMirren.
the class VxApiApplication method updtRoute.
/**
* 更新一个路由
*
* @param msg
*/
public void updtRoute(Message<JsonObject> msg) {
if (msg.body() == null) {
msg.fail(1400, "参数不能为空");
return;
}
VxApisDTO dto = VxApisDTO.fromJson(msg.body());
if (dto == null) {
msg.fail(1405, "参数不能为空");
return;
}
String apiName = dto.getApiName();
if (httpRouteMaps.get(apiName) != null) {
httpRouteMaps.get(apiName).forEach(r -> r.disable().remove());
}
if (httpsRouteMaps.get(apiName) != null) {
httpsRouteMaps.get(apiName).forEach(r -> r.disable().remove());
}
addRoute(msg);
}
Aggregations