package cn.web;

import static cn.constant.HttpConst.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.StringUtils;
import cn.api.websocket.AtvWebSocket;
import cn.util.core.LogKit;
import cn.util.time.TimeKit;

@ComponentScans({
	@ComponentScan(basePackages="cn.config"),
	@ComponentScan(basePackages="cn.component"),
	@ComponentScan(basePackages="cn.service"),
	@ComponentScan(basePackages="cn.api"),
})
@SpringBootApplication
@EnableTransactionManagement
@Configuration
public class WebApplication extends SpringBootServletInitializer
{
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(WebApplication.class);
	}
	
	public static void main(final String[] args) throws UnknownHostException 
	{
		//获取开始时间
		long start = System.currentTimeMillis();
	    ConfigurableApplicationContext application = SpringApplication.run(WebApplication.class, args);
        Environment env = application.getEnvironment();
		String protocol = HTTP;
		if (StringUtils.hasLength(env.getProperty(SERVER_SSL_KEY_STORE))) {
			protocol = HTTPS;
		}
		long end = System.currentTimeMillis();
		LogKit.info("\n----------------------------------------------------------\n\t"
				+ "服务名称:\t'{}' is running! \n\t"
				+ "本地访问:\t {}://localhost:{}/\n\t"  //Home/
				+ "外部访问:\t {}://{}:{}/\n\t" //Home/
				+ "上下文路径:\t {}\n\t"
				+ "运行环境:\t {}\n\t"
				+ "启动用时:\t {}\n\t"
				+ "启动时间:\t {}\n----------------------------------------------------------",
				!StringUtils.hasLength(env.getProperty("spring.application.name")) ? 
						    "Web服务器": env.getProperty("spring.application.name"),
							protocol, 
							env.getProperty("local.server.port"),
							protocol, 
							InetAddress.getLocalHost().getHostAddress(),
							env.getProperty("local.server.port"),
				            env.getProperty("server.servlet.context-path"),
							env.getActiveProfiles(), 
							(end - start) + "ms",
							TimeKit.getNowDate()
							);
		//解决WebSocket不能注入的问题
	    AtvWebSocket.setApplicationContext(application);
	}
}