package com.ng.ngtools;

import org.apache.catalina.connector.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication(scanBasePackages="com.ng.ngtools.modules")
@ComponentScan(basePackages = {"com.ng.ngtools.tangible","com.ng.ngtools.Tools"})
@EnableScheduling
@EnableAsync
@EnableCaching
public class NgApplication {

    public final static Logger log = LoggerFactory.getLogger(NgApplication.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext application = SpringApplication.run(NgApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = null; String httpsport ="";
        String httpport ="";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
            httpsport=  env.getProperty("server.port");
            httpport=  env.getProperty("myhttp-port");

        } catch (UnknownHostException e) {

        }
        log.info("\n----------------------------------------------------------\n\t" +
                "程序启动成功! URLs:\n\t" +
                "http: \t\thttp://"+ip+":" + httpport  +"\n\t" +
                "https: \t\thttps://"+ip +":"+ httpsport  +"\n\t" +
                "\n----------------------------------------------------------\n\t"
        );

    }

    @Value("${myhttp-port}")
    private Integer httpPort;
    /*SpringBoot 2.x版本(以及更高版本)使用下面的代码*/
    @Bean
    public ServletWebServerFactory servletcontainer(){
        TomcatServletWebServerFactory tomcat =new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
        return tomcat;
    }
    private Connector createHTTPConnector(){
        Connector connector =new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setSecure(false);
        connector.setPort(httpPort);
        return connector;
    }


}
