package com.games.gateway.utils;

import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zzhan
 * @Description 授权可访问的起始url校验
 * @Date 2023/10/27 15:11
 * @Version 1.0
 */
public class MatchStartUrlCheckUtils {

    /**
     * 配置各个服务可访问url路径前缀
     */
    private static final Map<String, List<String>> START_URL_CONFIG_MAP = new HashMap<>();

    static {

        //配置frog服务可访问url的路径
        START_URL_CONFIG_MAP.put("frog", Lists.newArrayList("/mobile/", "/web/", "/mock/", "/probe/"));

        //配置cow服务可访问url的路径
        START_URL_CONFIG_MAP.put("cow", Lists.newArrayList("/mobile/", "/web/", "/mock/", "/probe/"));

        //配置pigeon服务可访问url的路径
        START_URL_CONFIG_MAP.put("pigeon", Lists.newArrayList("/mobile/", "/web/", "/mock/", "/probe/", "/open-api/"));

        //配置shark服务可访问url的路径
        START_URL_CONFIG_MAP.put("shark", Lists.newArrayList("/mobile/", "/web/", "/mock/", "/probe/"));

        //配置lion服务可访问url的路径
        START_URL_CONFIG_MAP.put("lion", Lists.newArrayList("/mobile/", "/web/", "/mock/", "/probe/"));

    }


    /**
     * 校验不匹配访问url前缀的接口请求
     * @param applicationName
     * @param url
     * @return
     */
    public static boolean notMatchPrefixUrlCheck(String applicationName, String url) {
        List<String> prefixList = START_URL_CONFIG_MAP.get(applicationName);
        if(CollectionUtils.isEmpty(prefixList)) {
            return false;
        }
        for (String prefix : prefixList) {
            if(url.startsWith(prefix)) {
                return false;
            }
        }
        return true;
    }


}
