package com.willalp;

import org.apache.catalina.connector.Connector;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 启动程序
 *
 * @author willalp
 */

@EnableScheduling
@EnableAsync
@MapperScan(basePackages = {"com.willalp.**.mapper"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,})
public class WillalpApplication extends SpringBootServletInitializer {
    protected final static Logger logger = LoggerFactory.getLogger(WillalpApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(WillalpApplication.class, args);
        logger.info(" &&&&&&&&&&&&&&&&&&&&&& 会助理 - 梅陇 &&&&&&&&&&&&&&&&&&&&&& \n" +
                "__ |     / /  /_ _/  / /      / /     /   |    / /      / __ \\\n"
                + "__ | /| / /   / /   / /      / /     / /| |   / /      / /_/ /\n"
                + "__ |/ |/ /   / /   / /____  / /___  / /_| |  / /____  / ____/ \n"
                + "____/|__/  /___/  /______/ /_____/ /_/  |_| /______/ /_/");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WillalpApplication.class);
    }

    @Primary
    @Bean
    public TaskExecutor primaryTaskExecutor() {
        return new ThreadPoolTaskExecutor();
    }

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }

    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        int httpPort = 8080;
        connector.setPort(httpPort);
        return connector;
    }


    /*
    /**
     * http重定向到https
     *
     * @return
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的默认端口号
        connector.setPort(8080);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号,也就是项目配置的port
        connector.setRedirectPort(3005);
        return connector;
    }*/

}
