package org.dromara;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * springboot启动之后，打印版本信息
 *
 * @author shihongwei
 */
@Component
@Slf4j
public class SFrameworkStartup implements CommandLineRunner {

    public static final String VERSION = "v1.0.0";

    @Value("${spring.application.name:sa-plus}")
    private String applicationName;

    @Value("${server.port:8080}")
    private String port;

    @Value("${server.servlet.context-path:}")
    private String path;

    @Value("${spring.profiles.active:}")
    private String active;

    /**
     * 返回系统当前时间的YYYY-MM-dd hh:mm:ss 字符串格式
     */
    private static String getNow() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    @Override
    public void run(String... args) throws Exception {
        String ip = InetAddress.getLocalHost().getHostAddress();
        String str = "\n------------- " + applicationName + " [" + VERSION + "] (" + active + ") 启动成功 --by " + getNow() + " -------------\n" +
                "\t- Local:   http://localhost:" + port + path + "\n" +
                "\t- Local2:  http://127.0.0.1:" + port + path;
        log.info(str);
        //
        try {
            log.info("\t- Network: http://" + ip + ":" + port + path + "\n");
            log.info("Swagger:  http://" + ip + ":" + port + path + "swagger-ui/index.html\n");

            log.info("启动成功，Sa-Token 配置如下：\n" + SaManager.getConfig());

        } catch (Exception e) {
            log.error("\t- Network: 未能成功获取\n");
            log.error("异常：" + e.getMessage());
        }
    }

}

