Search in sources :

Example 1 with VxApis

use of com.szmirren.vxApi.core.entity.VxApis 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 2 with VxApis

use of com.szmirren.vxApi.core.entity.VxApis in project VX-API-Gateway by EliMirren.

the class VxApiApplication method addRouteToRouter.

/**
 * 通用给Router添加route
 *
 * @param api
 *          配置信息
 * @param router
 *          要添加的router
 * @param routeMaps
 *          要添加的route集合
 * @param result
 *          结果
 */
public void addRouteToRouter(VxApis api, Router router, Map<String, List<Route>> routeMaps, Handler<AsyncResult<Boolean>> result) {
    vertx.executeBlocking(fut -> {
        // 存储部署的路由
        List<Route> routes = new ArrayList<>();
        // 流量限制处理器
        if (api.getLimitUnit() != null) {
            // 权限认证的route;
            Route limitRoute = router.route();
            initApiLimit(api, limitRoute);
            routes.add(limitRoute);
        }
        // 入口参数检查
        if (api.getEnterParam() != null) {
            // 权限认证的route;
            Route checkRoute = router.route();
            initParamCheck(api, checkRoute);
            routes.add(checkRoute);
        }
        // 认证处理器
        if (api.getAuthOptions() != null) {
            // 权限认证的route;
            Route authRoute = router.route();
            try {
                initAuthHandler(api, authRoute);
                routes.add(authRoute);
            } catch (Exception e) {
                authRoute.remove();
                // 清空已经成功的路由
                routes.forEach(r -> r.remove());
                LOG.error("添加权限认证-->失败:" + e);
                fut.fail(e);
                return;
            }
        }
        // 前置处理器
        if (api.getBeforeHandlerOptions() != null) {
            // 前置处理器的route;
            Route beforeRoute = router.route();
            try {
                initBeforeHandler(api, beforeRoute);
                routes.add(beforeRoute);
            } catch (Exception e) {
                LOG.error("添加前置处理器-->失败:" + e);
                beforeRoute.remove();
                // 清空已经成功的路由
                routes.forEach(r -> r.remove());
                fut.fail(e);
                return;
            }
        }
        // 检查是否有后置处理器,有next给后置处理器,如果没有则response
        boolean isAfterHandler = api.getAfterHandlerOptions() != null;
        // 添加与后台交互的中心处理器
        Route serverRoute = router.route();
        try {
            initServerHandler(isAfterHandler, api, serverRoute);
            routes.add(serverRoute);
        } catch (Exception e) {
            LOG.error("添加服务处理器-->失败:" + e);
            serverRoute.remove();
            // 清空已经成功的路由
            routes.forEach(r -> r.remove());
            fut.fail(e);
            return;
        }
        // 后置处理器
        if (isAfterHandler) {
            // 前置处理器的route;
            Route afterRoute = router.route();
            try {
                initAfterHandler(api, afterRoute);
                routes.add(afterRoute);
            } catch (Exception e) {
                LOG.error("添加后置处理器-->失败:" + e);
                afterRoute.remove();
                // 清空已经成功的路由
                routes.forEach(r -> r.remove());
                fut.fail(e);
                return;
            }
        }
        // 添加异常处理器
        Route exRoute = router.route();
        initExceptionHanlder(api, exRoute);
        routes.add(exRoute);
        routeMaps.put(api.getApiName(), routes);
        fut.complete();
        if (LOG.isDebugEnabled()) {
            LOG.debug(appName + ": 服务器创建API成功");
        }
    }, result);
}
Also used : 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) ArrayList(java.util.ArrayList) Route(io.vertx.ext.web.Route) MalformedURLException(java.net.MalformedURLException)

Aggregations

StrUtil (com.szmirren.vxApi.core.common.StrUtil)2 VxApiEventBusAddressConstant (com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant)2 VxApiGatewayAttribute (com.szmirren.vxApi.core.common.VxApiGatewayAttribute)2 VxApiTrackInfos (com.szmirren.vxApi.core.entity.VxApiTrackInfos)2 VxApis (com.szmirren.vxApi.core.entity.VxApis)2 ApiServerTypeEnum (com.szmirren.vxApi.core.enums.ApiServerTypeEnum)2 HttpMethodEnum (com.szmirren.vxApi.core.enums.HttpMethodEnum)2 VxApiRouteHandlerApiLimit (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerApiLimit)2 VxApiRouteHandlerHttpService (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerHttpService)2 VxApiRouteHandlerParamCheck (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerParamCheck)2 VxApiRouteHandlerRedirectType (com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerRedirectType)2 VxApiApplicationDTO (com.szmirren.vxApi.core.options.VxApiApplicationDTO)2 VxApiApplicationOptions (com.szmirren.vxApi.core.options.VxApiApplicationOptions)2 VxApiCertOptions (com.szmirren.vxApi.core.options.VxApiCertOptions)2 VxApiCorsOptions (com.szmirren.vxApi.core.options.VxApiCorsOptions)2 VxApiServerOptions (com.szmirren.vxApi.core.options.VxApiServerOptions)2 VxApisDTO (com.szmirren.vxApi.core.options.VxApisDTO)2 VxApiAuth (com.szmirren.vxApi.spi.auth.VxApiAuth)2 VxApiAuthFactory (com.szmirren.vxApi.spi.auth.VxApiAuthFactory)2 VxApiAuthOptions (com.szmirren.vxApi.spi.auth.VxApiAuthOptions)2