package main

import (
	"pbx-api-gin/internal/app"
	"pbx-api-gin/internal/pkg/configs"
	"pbx-api-gin/pkg/lfshook"
	"pbx-api-gin/pkg/utils"

	"github.com/sirupsen/logrus"
	"gopkg.in/natefinch/lumberjack.v2"
)

var (
	gitCommitCode string
	buildDateTime string
	goVersion     string
)

func main() {
	initVersion()

	// 解析配置文件
	configs.ConfigPath = "/data/test/configs/config.yaml"
	configs.DecodeConfig()

	configs.ConfigGlobal.LogLevel = logrus.InfoLevel

	//gin.SetMode(gin.ReleaseMode)
	//gin.SetMode(gin.DebugMode)

	lfshook.NewLogger().Logger.SetFormatter(&logrus.TextFormatter{
		ForceQuote:      false,
		FullTimestamp:   true,
		TimestampFormat: "2006-01-02 15:04:05",
	})

	if configs.ConfigGlobal.LogLevel < logrus.DebugLevel {

		pathMap := lfshook.LoggerMap{
			logrus.InfoLevel: &lumberjack.Logger{
				Filename:   configs.ConfigGlobal.LogInfoPath,
				MaxSize:    10, // maxSize M
				MaxBackups: 5,  // keep 5 file
				MaxAge:     7,  //  7 day
			},

			logrus.ErrorLevel: &lumberjack.Logger{
				Filename:   configs.ConfigGlobal.LogErrorPath,
				MaxSize:    10, // maxSize M
				MaxBackups: 5,  // keep 5 file
				MaxAge:     7,  //  7 day
			},
		}

		lfshook.NewLogger().Logger.Hooks.Add(lfshook.NewHook(
			pathMap,
			&logrus.TextFormatter{
				DisableColors:   true,
				ForceQuote:      true,
				TimestampFormat: "2006-01-02 15:04:05",
				PadLevelText:    false,
			},
		))
		lfshook.NewLogger().Logger.SetReportCaller(true)

	}

	app.StartApp()
	utils.Exit()
	//return nil
}

func initVersion() {
	if gitCommitCode != "" {
		//构建信息
		//utils.Logger.Printf("software version: V10.01")
		lfshook.NewLogger().Printf("git commit code: %s", gitCommitCode)
		lfshook.NewLogger().Printf("build date: %s", buildDateTime)
		lfshook.NewLogger().Printf("go version: %s", goVersion)

		utils.VersionInstance.BuildDate = buildDateTime
		utils.VersionInstance.GoVersion = goVersion
		utils.VersionInstance.GitCommitCode = gitCommitCode
	}
}
