Search in sources :

Example 1 with VxApisDTO

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()));
        }
    });
}
Also used : User(io.vertx.ext.auth.User) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO)

Example 2 with VxApisDTO

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()));
        }
    });
}
Also used : User(io.vertx.ext.auth.User) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO)

Example 3 with VxApisDTO

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编写是否正确");
    }
}
Also used : VxApis(com.szmirren.vxApi.core.entity.VxApis) VxApiServerOptions(com.szmirren.vxApi.core.options.VxApiServerOptions) VxApiBeforeHandler(com.szmirren.vxApi.spi.handler.VxApiBeforeHandler) HttpMethodEnum(com.szmirren.vxApi.core.enums.HttpMethodEnum) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VxApiEventBusAddressConstant(com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant) ApiServerTypeEnum(com.szmirren.vxApi.core.enums.ApiServerTypeEnum) VxApiAuthFactory(com.szmirren.vxApi.spi.auth.VxApiAuthFactory) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Map(java.util.Map) VxApiAfterHandler(com.szmirren.vxApi.spi.handler.VxApiAfterHandler) VxApiAfterHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerFactory) JsonObject(io.vertx.core.json.JsonObject) CookieHandler(io.vertx.ext.web.handler.CookieHandler) VxApiApplicationOptions(com.szmirren.vxApi.core.options.VxApiApplicationOptions) ClusteredSessionStore(io.vertx.ext.web.sstore.ClusteredSessionStore) VxApiCustomHandler(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandler) Set(java.util.Set) Message(io.vertx.core.eventbus.Message) VxApiTrackInfos(com.szmirren.vxApi.core.entity.VxApiTrackInfos) Future(io.vertx.core.Future) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO) VxApis(com.szmirren.vxApi.core.entity.VxApis) List(java.util.List) VxApiGatewayAttribute(com.szmirren.vxApi.core.common.VxApiGatewayAttribute) VxApiAuth(com.szmirren.vxApi.spi.auth.VxApiAuth) Logger(org.apache.logging.log4j.Logger) VxApiAfterHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerOptions) StrUtil(com.szmirren.vxApi.core.common.StrUtil) SessionStore(io.vertx.ext.web.sstore.SessionStore) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO) VxApiCustomHandlerOptions(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerOptions) HttpClient(io.vertx.core.http.HttpClient) VxApiCustomHandlerFactory(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerFactory) VxApiRouteHandlerApiLimit(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerApiLimit) LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) VxApiRouteHandlerRedirectType(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerRedirectType) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PfxOptions(io.vertx.core.net.PfxOptions) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) VxApiBeforeHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerOptions) SessionHandler(io.vertx.ext.web.handler.SessionHandler) VxApiRouteHandlerParamCheck(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerParamCheck) VxApiCertOptions(com.szmirren.vxApi.core.options.VxApiCertOptions) AsyncResult(io.vertx.core.AsyncResult) LinkedHashSet(java.util.LinkedHashSet) Route(io.vertx.ext.web.Route) VxApiCorsOptions(com.szmirren.vxApi.core.options.VxApiCorsOptions) MalformedURLException(java.net.MalformedURLException) VxApiAuthOptions(com.szmirren.vxApi.spi.auth.VxApiAuthOptions) JsonArray(io.vertx.core.json.JsonArray) VxApiBeforeHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerFactory) HttpMethod(io.vertx.core.http.HttpMethod) VxApiRouteHandlerHttpService(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerHttpService) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) CorsHandler(io.vertx.ext.web.handler.CorsHandler) JsonObject(io.vertx.core.json.JsonObject) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO)

Example 4 with VxApisDTO

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);
}
Also used : VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO)

Aggregations

VxApisDTO (com.szmirren.vxApi.core.options.VxApisDTO)4 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 JsonObject (io.vertx.core.json.JsonObject)3 StrUtil (com.szmirren.vxApi.core.common.StrUtil)1 VxApiEventBusAddressConstant (com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant)1 VxApiGatewayAttribute (com.szmirren.vxApi.core.common.VxApiGatewayAttribute)1 VxApiTrackInfos (com.szmirren.vxApi.core.entity.VxApiTrackInfos)1 VxApis (com.szmirren.vxApi.core.entity.VxApis)1 ApiServerTypeEnum (com.szmirren.vxApi.core.enums.ApiServerTypeEnum)1 HttpMethodEnum (com.szmirren.vxApi.core.enums.HttpMethodEnum)1 VxApiRouteHandlerApiLimit (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerApiLimit)1 VxApiRouteHandlerHttpService (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerHttpService)1 VxApiRouteHandlerParamCheck (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerParamCheck)1 VxApiRouteHandlerRedirectType (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerRedirectType)1 VxApiApplicationDTO (com.szmirren.vxApi.core.options.VxApiApplicationDTO)1 VxApiApplicationOptions (com.szmirren.vxApi.core.options.VxApiApplicationOptions)1 VxApiCertOptions (com.szmirren.vxApi.core.options.VxApiCertOptions)1 VxApiCorsOptions (com.szmirren.vxApi.core.options.VxApiCorsOptions)1 VxApiServerOptions (com.szmirren.vxApi.core.options.VxApiServerOptions)1 VxApiAuth (com.szmirren.vxApi.spi.auth.VxApiAuth)1