Search in sources :

Example 1 with VxApiApplicationDTO

use of com.szmirren.vxApi.core.options.VxApiApplicationDTO in project VX-API-Gateway by EliMirren.

the class ClientVerticle method addAPP.

/**
 * 添加应用
 *
 * @param rct
 */
public void addAPP(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")));
                VxApiApplicationDTO dto = VxApiApplicationDTO.fromJson(rct.getBodyAsJson());
                JsonObject param = new JsonObject();
                param.put("appName", dto.getAppName());
                param.put("app", dto.toJson().put("time", Instant.now()));
                vertx.eventBus().<Integer>send(thisVertxName + VxApiEventBusAddressConstant.ADD_APP, param, cres -> {
                    if (cres.succeeded()) {
                        response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, cres.result().body()));
                        LOG.info(MessageFormat.format("[user : {0}] 执行添加应用-->结果: {1}", rct.session().<String>get("userName"), cres.result().body()));
                    } else {
                        LOG.error(MessageFormat.format("[user : {0}] 执行添加应用-->失败:{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}] 执行添加应用-->失败:未授权或者无权利", rct.session().get("userName")));
                response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C401));
            }
        } else {
            LOG.error(MessageFormat.format("[user : {0}] 执行添加应用-->失败:{1}", rct.session().get("userName"), res.cause()));
            response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, res.cause().getMessage()));
        }
    });
}
Also used : User(io.vertx.ext.auth.User) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO)

Example 2 with VxApiApplicationDTO

use of com.szmirren.vxApi.core.options.VxApiApplicationDTO in project VX-API-Gateway by EliMirren.

the class ClientVerticle method updtAPP.

/**
 * 修改一个应用
 *
 * @param rct
 */
public void updtAPP(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")));
                VxApiApplicationDTO dto = VxApiApplicationDTO.fromJson(rct.getBodyAsJson());
                JsonObject param = new JsonObject();
                param.put("appName", dto.getAppName());
                param.put("app", dto.toJson());
                vertx.eventBus().<Integer>send(thisVertxName + VxApiEventBusAddressConstant.UPDT_APP, param, cres -> {
                    if (cres.succeeded()) {
                        response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C200, cres.result().body()));
                        LOG.info(MessageFormat.format("[user : {0}] 执行修改应用:{2}-->结果: {1}", rct.session().<String>get("userName"), cres.result().body(), dto.getAppName()));
                    } else {
                        LOG.error(MessageFormat.format("[user : {0}] 执行修改应用-->失败:{1}", rct.session().get("userName"), cres.cause()));
                        response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, cres.cause().toString()));
                    }
                });
            } else {
                LOG.error(MessageFormat.format("[user : {0}] 执行修改应用-->失败:未授权或者无权利", rct.session().get("userName")));
                response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C401));
            }
        } else {
            LOG.error(MessageFormat.format("[user : {0}] 执行修改应用-->失败:{1}", rct.session().get("userName"), res.cause()));
            response.end(ResultFormat.format(HTTPStatusCodeMsgEnum.C500, res.cause().getMessage()));
        }
    });
}
Also used : User(io.vertx.ext.auth.User) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO)

Example 3 with VxApiApplicationDTO

use of com.szmirren.vxApi.core.options.VxApiApplicationDTO in project VX-API-Gateway by EliMirren.

the class VxApiApplication method start.

@Override
public void start(Future<Void> startFuture) throws Exception {
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("加载应用配置信息...");
        }
        thisVertxName = System.getProperty("thisVertxName", "VX-API");
        VxApiApplicationDTO app = VxApiApplicationDTO.fromJson(config().getJsonObject("appConfig"));
        this.appOption = new VxApiApplicationOptions(app);
        appName = appOption.getAppName();
        this.serverOptions = appOption.getServerOptions();
        if (LOG.isDebugEnabled()) {
            LOG.debug("加载全局黑名单");
        }
        config().getJsonArray("blackIpSet").forEach(ip -> {
            if (ip instanceof String) {
                blackIpSet.add(ip.toString());
            }
        });
        if (LOG.isDebugEnabled()) {
            LOG.debug("加载跨域处理信息");
        }
        this.corsOptions = appOption.getCorsOptions();
        if (appOption == null) {
            LOG.error("创建应用程序-->失败:配置信息为空");
            startFuture.fail("创建应用程序失败:配置信息为空");
            return;
        } else {
            this.httpClient = vertx.createHttpClient(appOption);
            Future<Void> httpFuture = Future.future(future -> {
                createHttpServer(res -> {
                    if (res.succeeded()) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("实例化应用程序->创建HTTP服务器-->成功!");
                        }
                        future.complete();
                    } else {
                        LOG.error("实例化应用程序->创建HTTP服务器-->失败:" + res.cause());
                        future.fail(res.cause());
                    }
                });
            });
            Future<Void> httpsFuture = Future.future(futrue -> {
                createHttpsServer(res -> {
                    if (res.succeeded()) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("实例化应用程序->创建HTTPS服务器-->成功!");
                        }
                        futrue.complete();
                    } else {
                        LOG.error("实例化应用程序->创建HTTPS服务器-->失败:" + res.cause());
                        futrue.fail(res.cause());
                    }
                });
            });
            Future<Void> eventFutrue = Future.future(future -> {
                // 注册操作地址
                vertx.eventBus().consumer(thisVertxName + appOption.getAppName() + VxApiEventBusAddressConstant.APPLICATION_ADD_API_SUFFIX, this::addRoute);
                vertx.eventBus().consumer(thisVertxName + appOption.getAppName() + VxApiEventBusAddressConstant.APPLICATION_DEL_API_SUFFIX, this::delRoute);
                vertx.eventBus().consumer(VxApiEventBusAddressConstant.SYSTEM_PUBLISH_BLACK_IP_LIST, this::updateIpBlackList);
                future.complete();
            });
            CompositeFuture.all(httpFuture, httpsFuture, eventFutrue).setHandler(res -> {
                if (res.succeeded()) {
                    LOG.info("实例化应用程序-->成功");
                    startFuture.complete();
                } else {
                    LOG.error("实例化应用程序-->失败:", res.cause());
                    startFuture.fail(res.cause());
                }
            });
        }
    } catch (Exception e) {
        LOG.error("实例化应用程序-->失败:", e);
        startFuture.fail(e);
    }
}
Also used : VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO) VxApiApplicationOptions(com.szmirren.vxApi.core.options.VxApiApplicationOptions) MalformedURLException(java.net.MalformedURLException)

Aggregations

VxApiApplicationDTO (com.szmirren.vxApi.core.options.VxApiApplicationDTO)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)2 JsonObject (io.vertx.core.json.JsonObject)2 User (io.vertx.ext.auth.User)2 VxApiApplicationOptions (com.szmirren.vxApi.core.options.VxApiApplicationOptions)1 MalformedURLException (java.net.MalformedURLException)1