package main

import (
	"database/sql"
	"sundynix-go/core"
	"sundynix-go/global"
	"sundynix-go/initialize"
	"sundynix-go/pkg/httpclient"

	"go.uber.org/zap"
)

// @title                       Swagger API接口文档
// @version                     v1.0.0
// @description                 使用gin + gorm进行极速开发的全栈开发基础平台
// @securityDefinitions.apikey  BearerAuth
// @in                          header
// @name                        Authorization
// @BasePath                    /
func main() {
	//viper
	global.Viper = core.Viper()
	//canzap
	global.Logger = core.Zap()
	//swap
	zap.ReplaceGlobals(global.Logger)
	//初始化Gorm 连接数据库
	global.DB = initialize.Gorm()
	//redis
	initialize.Redis()

	// timer
	initialize.InitTimer()
	// httpclient 主动初始化 HTTP Client（可选，也可依赖懒加载）饿汉加载
	httpclient.InitHttpClient()

	//迁移数据库
	if global.DB != nil {
		initialize.MigrateTable() // 迁移数据库结构
		db, _ := global.DB.DB()
		defer func(db *sql.DB) {
			err := db.Close()
			if err != nil {
				global.Logger.Error("db close failed", zap.Error(err))
			}
		}(db)
	}

	//初始化路由
	initialize.Routers()
}
