package main

import (
	"runtime"
	"time"

	"go.uber.org/zap"
	"courseSign/server/utils/chain33"

	"courseSign/server/core"
	_ "courseSign/server/cron"
	"courseSign/server/global"
	"courseSign/server/initialize"
)

//go:generate go env -w GO111MODULE=on
//go:generate go env -w GOPROXY=https://goproxy.cn,direct
//go:generate go mod tidy
//go:generate go mod download

// @title                       Swagger Example API
// @version                     0.0.1
// @description                 This is a sample Server pets
// @securityDefinitions.apikey  ApiKeyAuth
// @in                          header
// @name                        x-token
// @BasePath                    /
func main() {
	global.GVA_VP = core.Viper() // 初始化Viper
	initialize.OtherInit()
	global.GVA_LOG = core.Zap() // 初始化zap日志库
	zap.ReplaceGlobals(global.GVA_LOG)
	global.GVA_DB = initialize.Gorm() // gorm连接数据库
	if global.GVA_CONFIG.BlockChain.MainHost != "" {
		global.Client,_ = chain33.NewGrpcClient(global.GVA_CONFIG.BlockChain.MainHost)
	}
	initialize.Timer()
	initialize.DBList()
	if global.GVA_DB != nil {
		initialize.RegisterTables() // 初始化表
		// 程序结束前关闭数据库链接
		db, _ := global.GVA_DB.DB()
		defer db.Close()
	}
	t := time.Tick(30 * time.Second)
	go func() {
		for range t {
			var m runtime.MemStats
			runtime.ReadMemStats(&m)
			global.GVA_LOG.Info("info:NumGoroutine:", zap.Int("num ",runtime.NumGoroutine()))
			global.GVA_LOG.Info("info:Mem:%d",  zap.Uint64("num ",m.Sys/(1024*1024)))
			global.GVA_LOG.Info("info:HeapAlloc:%d",  zap.Uint64("num ",m.HeapAlloc/(1024*1024)))
		}
	}()
	core.RunWindowsServer()
}
