package cn.com.capcloud;

import cn.com.capcloud.core.noentitydb.StartupGuard;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.net.InetAddress;

/**
 * 启动类
 */
@ServletComponentScan
@EnableScheduling
@EnableAsync
@EnableWebMvc
@SpringBootApplication(exclude = {
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
public class App {

  private static final Logger logger = LoggerFactory.getLogger(App.class);

  public static void main(String[] args) {
    try {
      StartupGuard.checkDatabaseOrThrow();
      ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
      ConfigurableEnvironment environment = context.getEnvironment();
      String hostAddress = InetAddress.getLocalHost().getHostAddress();
      String port = environment.getProperty("server.port");
      String contextPath = environment.getProperty("server.servlet.context-path");
      logger.info("启动成功，接口地址为：http://{}:{}{}", hostAddress, port, contextPath == null ? "" : contextPath);
    } catch (Exception e) {
      logger.error("启动失败", e);
    }
  }

  @Bean
  public ModelMapper modelMapper() {
    return new ModelMapper();
  }
}
