package com.zhiqiyun.open;

import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @author stjdydayou
 */
@Slf4j
@Configuration
@ServletComponentScan
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }


    @Bean
    public DbSearcher dbSearcher() throws Exception {

        DbConfig config = new DbConfig();

        InputStream inStream = Application.class.getResourceAsStream("/ip2region.db");

        byte[] buff = new byte[1024];

        ByteArrayOutputStream o = new ByteArrayOutputStream();

        int rc = 0;
        while ((rc = inStream.read(buff, 0, 1024)) > 0) {
            o.write(buff, 0, rc);
        }

        byte[] in2b = o.toByteArray();

        return new DbSearcher(config, in2b);
    }
}
