All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.api.server.AbstractApiGather Maven / Gradle / Ivy

There is a newer version: 10.0.12
Show newest version
package shz.api.server;

import com.alibaba.fastjson2.JSON;
import shz.core.FieldSetter;
import shz.core.Help;
import shz.core.id.IdInfo;
import shz.core.id.SFIdProducer;
import shz.core.model.PageInfo;
import shz.core.msg.ClientFailureMsg;
import shz.core.msg.ServerFailureMsg;
import shz.core.structure.RateLimiter;
import shz.jdbc.JdbcService;
import shz.spring.BeanContainer;
import shz.spring.api.ApiInfo;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public abstract class AbstractApiGather {
    private static final ApiInfo apiDetail = ApiInfo.of();

    protected final String appName;
    protected final JdbcService jdbcService;

    protected AbstractApiGather(String appName, JdbcService jdbcService) {
        this.appName = appName;
        this.jdbcService = jdbcService;
    }

    protected AbstractApiGather(String appName, DataSource dataSource) {
        this.appName = appName;
        jdbcService = new JdbcService();
        jdbcService.setDataSource(dataSource);
        jdbcService.setIdProducer(new SFIdProducer(new IdInfo()));
    }

    protected AbstractApiGather(String appName, String driverClassName, String url, String username, String password) {
        this.appName = appName;
        jdbcService = new JdbcService();
        jdbcService.setDataSource(driverClassName, url, username, password);
        jdbcService.setIdProducer(new SFIdProducer(new IdInfo()));
    }

    @PostConstruct
    void init() {
        if (auto()) execute();
    }

    protected boolean auto() {
        return true;
    }

    /**
     * 收集项目api接口
     */
    public final void execute() {
        BeanContainer.set(() -> {
            List apis = ApiInfo.generate(dto -> {
                SysApi sysApi = FieldSetter.create(dto, SysApi.class);
                sysApi.setAppName(appName);
                sysApi.setReq(JSON.toJSONString(dto.getReqVo()));
                sysApi.setRes(JSON.toJSONString(dto.getResVo()));
                return sysApi;
            });

            if (apis.size() > 0) {
                List deletedApiIds = new LinkedList<>();
                jdbcService.init(SysApi.class, apis, SysApi::getId,
                        sysApi -> Help.equals(appName, sysApi.getAppName()),
                        this::existApi,
                        sysApi -> deletedApiIds.add(sysApi.getId()),
                        "appName", "path", "method"
                );
                if (!deletedApiIds.isEmpty()) afterDelete(deletedApiIds);
            }
        });
    }

    private void existApi(SysApi sysApi) {
        ApiDetail detail = new ApiDetail();
        setApiDetail(detail, sysApi);
        apiDetail.put(sysApi.getMethod(), sysApi.getPath(), detail);
    }

    private void setApiDetail(ApiDetail detail, SysApi sysApi) {
        detail.setPermission(sysApi.getPermission());
        detail.setLogin(sysApi.getLogin());
        detail.setEnabled(sysApi.getEnabled());

        RateLimiter rateLimiter = detail.getRateLimiter();
        if (rateLimiter == null) {
            rateLimiter = new RateLimiter();
            detail.setRateLimiter(rateLimiter);
        }
        rateLimiter.setPermits(sysApi.getQpsLimit());

        detail.setReusable(sysApi.getReusable());
        detail.setRepeatedLimit(sysApi.getRepeatedLimit());
        detail.setRecord(sysApi.getRecord());
    }

    /**
     * 处理删除之后的api
     */
    protected void afterDelete(List deletedApiIds) {
        //to do something
    }

    /**
     * 获取api详情(鉴权及限流等)
     */
    public final ApiDetail getApiDetail(String method, String path) {
        return apiDetail.get(method, path);
    }

    /**
     * 接口列表
     */
    public final List list(Query query) {
        query.setAppName(appName);
        return jdbcService.selectList(SysApi.class, jdbcService.where(SysApi.class, query), null, jdbcService.orderBys(SysApi.class, query), null);
    }

    /**
     * 接口分页列表
     */
    public final PageInfo page(PageInfo pageInfo, Query query) {
        query.setAppName(appName);
        return jdbcService.page(pageInfo, SysApi.class, jdbcService.where(SysApi.class, query), null, jdbcService.orderBys(SysApi.class, query), null);
    }

    /**
     * 更新接口
     */
    public final void update(SysApi sysApi) {
        SysApi oldEntity = jdbcService.selectById(SysApi.class, sysApi.getId());
        ClientFailureMsg.requireNonNull(oldEntity, "接口不存在");

        ApiDetail detail = getApiDetail(oldEntity.getMethod(), oldEntity.getPath());
        ClientFailureMsg.requireNonNull(detail, "当前应用不存在该接口");

        int row = jdbcService.updateById(sysApi);
        ServerFailureMsg.requireNon(jdbcService.fail(row), "更新接口失败");

        if (sysApi.getEnabled() != null) oldEntity.setEnabled(sysApi.getEnabled());
        if (sysApi.getQpsLimit() != null) oldEntity.setQpsLimit(sysApi.getQpsLimit());
        if (sysApi.getReusable() != null) oldEntity.setReusable(sysApi.getReusable());
        if (sysApi.getRepeatedLimit() != null) oldEntity.setRepeatedLimit(sysApi.getRepeatedLimit());
        if (sysApi.getRecord() != null) oldEntity.setRecord(sysApi.getRecord());

        //刷新缓存
        setApiDetail(detail, oldEntity);
    }

    /**
     * 接口下拉(用于权限配置)
     */
    public final List dropDown() {
        Query query = new Query();
        query.setAppName(appName);
        query.setPermission(Arrays.asList(1, Integer.MAX_VALUE));
        return jdbcService.selectList(SysApi.class, jdbcService.where(query), null, jdbcService.orderBys(SysApi.class, query), null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy