package com.zd.common.swagger.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

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

/**
 * @Author: zhoupan
 * @Date: 2021/09/30/10:13
 * @Description:
 */
public class AppListener implements ApplicationListener<ApplicationStartedEvent> {
    static Logger logger= LoggerFactory.getLogger(AppListener.class);
    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent)  {

        Environment env = applicationStartedEvent.getApplicationContext().getEnvironment();
        try {
            logger.info("\n----------------------------------------------------------\n\t" +
                            "Application '{}' is running! Access URLs:\n\t" +
                            "Local: \t\thttp://localhost:{}\n\t" +
                            "External: \thttp://{}:{}\n\t"+
                            "接口文档地址: \thttp://{}:{}/doc.html\n\t"+
                            "----------------------------------------------------------",
                    env.getProperty("spring.application.name"),
                    env.getProperty("server.port"),
                    InetAddress.getLocalHost().getHostAddress(),
                    env.getProperty("server.port"),
                    InetAddress.getLocalHost().getHostAddress(),
                    env.getProperty("server.port")
            );
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
