package xtools.boot.ip.init;

import org.jspecify.annotations.NonNull;
import org.lionsoul.ip2region.xdb.Version;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import xtools.base.config.BaseParams;
import xtools.boot.ip.utils.IpUtils;
import xtools.extend.IpLocalUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>Title : InitIp</p>
 * <p>Description : InitIp</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2026/2/16 10:21
 */
@Component
@Order(BaseParams.CP_NUM50)
public class InitIp implements ApplicationRunner {

    private static final String IP_FILE = "ip/ip2region_v4.xdb";

    @Override
    public void run(@NonNull ApplicationArguments args) throws Exception {
        init();
    }

    /**
     * 初始化IP库
     */
    private void init() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(IP_FILE);
        try (InputStream is = classPathResource.getInputStream()) {
            IpLocalUtils.init(is, Version.IPv4);
            IpUtils.init();
        }
    }
}
