Search in sources :

Example 6 with AddApiReqVo

use of com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo in project XHuiCloud by sindaZeng.

the class GatewayApiController method addApi.

@PostMapping("/new.json")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo) {
    String app = reqVo.getApp();
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    ApiDefinitionEntity entity = new ApiDefinitionEntity();
    entity.setApp(app.trim());
    String ip = reqVo.getIp();
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    entity.setIp(ip.trim());
    Integer port = reqVo.getPort();
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    entity.setPort(port);
    // API名称
    String apiName = reqVo.getApiName();
    if (StringUtil.isBlank(apiName)) {
        return Result.ofFail(-1, "apiName can't be null or empty");
    }
    entity.setApiName(apiName.trim());
    // 匹配规则列表
    List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
    if (CollectionUtils.isEmpty(predicateItems)) {
        return Result.ofFail(-1, "predicateItems can't empty");
    }
    List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
    for (ApiPredicateItemVo predicateItem : predicateItems) {
        ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
        // 匹配模式
        Integer matchStrategy = predicateItem.getMatchStrategy();
        if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
            return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
        }
        predicateItemEntity.setMatchStrategy(matchStrategy);
        // 匹配串
        String pattern = predicateItem.getPattern();
        if (StringUtil.isBlank(pattern)) {
            return Result.ofFail(-1, "pattern can't be null or empty");
        }
        predicateItemEntity.setPattern(pattern);
        predicateItemEntities.add(predicateItemEntity);
    }
    entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
    // 检查API名称不能重复
    List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
    if (allApis.stream().map(o -> o.getApiName()).anyMatch(o -> o.equals(apiName.trim()))) {
        return Result.ofFail(-1, "apiName exists: " + apiName);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add gateway api error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishApis(app, ip, port)) {
        logger.warn("publish gateway apis fail after add");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction) Result(com.alibaba.csp.sentinel.dashboard.domain.Result) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) java.util(java.util) Logger(org.slf4j.Logger) AuthService(com.alibaba.csp.sentinel.dashboard.auth.AuthService) InMemApiDefinitionStore(com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore) SentinelGatewayConstants(com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) StringUtil(com.alibaba.csp.sentinel.util.StringUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) SentinelApiClient(com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient) AddApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo) CollectionUtils(org.springframework.util.CollectionUtils) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) UpdateApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 7 with AddApiReqVo

use of com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo in project RuoYi-Cloud-Plus by JavaLionLi.

the class GatewayApiController method addApi.

@PostMapping("/new.json")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo) {
    String app = reqVo.getApp();
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    ApiDefinitionEntity entity = new ApiDefinitionEntity();
    entity.setApp(app.trim());
    String ip = reqVo.getIp();
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    entity.setIp(ip.trim());
    Integer port = reqVo.getPort();
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    entity.setPort(port);
    // API名称
    String apiName = reqVo.getApiName();
    if (StringUtil.isBlank(apiName)) {
        return Result.ofFail(-1, "apiName can't be null or empty");
    }
    entity.setApiName(apiName.trim());
    // 匹配规则列表
    List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
    if (CollectionUtils.isEmpty(predicateItems)) {
        return Result.ofFail(-1, "predicateItems can't empty");
    }
    List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
    for (ApiPredicateItemVo predicateItem : predicateItems) {
        ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
        // 匹配模式
        Integer matchStrategy = predicateItem.getMatchStrategy();
        if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
            return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
        }
        predicateItemEntity.setMatchStrategy(matchStrategy);
        // 匹配串
        String pattern = predicateItem.getPattern();
        if (StringUtil.isBlank(pattern)) {
            return Result.ofFail(-1, "pattern can't be null or empty");
        }
        predicateItemEntity.setPattern(pattern);
        predicateItemEntities.add(predicateItemEntity);
    }
    entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
    // 检查API名称不能重复
    List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
    if (allApis.stream().map(o -> o.getApiName()).anyMatch(o -> o.equals(apiName.trim()))) {
        return Result.ofFail(-1, "apiName exists: " + apiName);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add gateway api error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishApis(app, ip, port)) {
        logger.warn("publish gateway apis fail after add");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction) Result(com.alibaba.csp.sentinel.dashboard.domain.Result) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) java.util(java.util) Logger(org.slf4j.Logger) AuthService(com.alibaba.csp.sentinel.dashboard.auth.AuthService) InMemApiDefinitionStore(com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore) SentinelGatewayConstants(com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) StringUtil(com.alibaba.csp.sentinel.util.StringUtil) HttpServletRequest(javax.servlet.http.HttpServletRequest) SentinelApiClient(com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient) AddApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo) CollectionUtils(org.springframework.util.CollectionUtils) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) UpdateApiReqVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo) ApiDefinitionEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity) ApiPredicateItemVo(com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo) ApiPredicateItemEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Aggregations

ApiDefinitionEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity)7 ApiPredicateItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity)7 Result (com.alibaba.csp.sentinel.dashboard.domain.Result)7 AddApiReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo)7 ApiPredicateItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo)7 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)5 AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)5 AuthService (com.alibaba.csp.sentinel.dashboard.auth.AuthService)5 SentinelApiClient (com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient)5 MachineInfo (com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo)5 UpdateApiReqVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo)5 InMemApiDefinitionStore (com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore)5 StringUtil (com.alibaba.csp.sentinel.util.StringUtil)5 java.util (java.util)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 CollectionUtils (org.springframework.util.CollectionUtils)5 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)5