package main

import (
	"ColdVerify_local/Nats"
	_ "ColdVerify_local/Nats"
	"ColdVerify_local/conf"
	_ "ColdVerify_local/routers"
	"fmt"
	"github.com/beego/beego/v2/adapter/orm"
	orm2 "github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/filter/cors"
	_ "github.com/go-sql-driver/mysql"
	"runtime"
	"strconv"
	"time"
)

func init() {
	fmt.Println(runtime.GOOS)
	orm.RegisterDriver("mysql", orm.DRMySQL)
	//orm.RegisterDataBase("default", "mysql", "zdxq:7e5853d9178edfcc@tcp(47.108.133.234:3306)/zdxq?charset=utf8",100,200)
	orm.RegisterDataBase(conf.Local_AliasName, "mysql",
		conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
		conf.MysqlServer_MaxIdleConnections, conf.MysqlServer_MaxOpenConnections)
	orm.RunSyncdb(conf.Local_AliasName, false, true) // 创建本地数据库
	orm2.ConnMaxLifetime(30 * time.Minute)
	println(conf.MysqlServer_Username + ":" + conf.MysqlServer_Password + "@tcp(" + conf.MysqlServer_UrlPort + ")/" + conf.MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True")

	orm.RegisterDataBase(conf.Server_AliasName, "mysql",
		conf.MysqlServer2_Username+":"+conf.MysqlServer2_Password+"@tcp("+conf.MysqlServer2_UrlPort+")/"+conf.MysqlServer2_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
		conf.MysqlServer2_MaxIdleConnections, conf.MysqlServer2_MaxOpenConnections)
	println(conf.MysqlServer2_Username + ":" + conf.MysqlServer2_Password + "@tcp(" + conf.MysqlServer2_UrlPort + ")/" + conf.MysqlServer2_Database + "?charset=utf8mb4&loc=Local&parseTime=True")

	orm.RegisterDataBase(conf.Verify1_AliasName, "mysql",
		conf.MysqlVerify_Username+":"+conf.MysqlVerify_Password+"@tcp("+conf.MysqlVerify_UrlPort+")/"+conf.MysqlVerify_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
		conf.MysqlServer2_MaxIdleConnections, conf.MysqlServer2_MaxOpenConnections)
	println(conf.MysqlVerify_Username + ":" + conf.MysqlVerify_Password + "@tcp(" + conf.MysqlVerify_UrlPort + ")/" + conf.MysqlVerify_Database + "?charset=utf8mb4&loc=Local&parseTime=True")
	orm2.Debug = true
	orm.Debug = true
}

func main() {

	HTTPPort, _ := beego.AppConfig.String("HTTPPort")
	HTTPPort_int, _ := strconv.Atoi(HTTPPort)

	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		// 允许访问所有源
		AllowAllOrigins: true,
		// 可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有)
		AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		// 指的是允许的Header的种类
		AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		// 公开的HTTP标头列表
		ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		// 如果设置，则允许共享身份验证凭据，例如cookie
		AllowCredentials: true,
	}))

	beego.BConfig.AppName = conf.AppName                    // 项目名
	beego.BConfig.ServerName = conf.AppName + conf.HTTPPort //server  名称
	beego.BConfig.RunMode = "dev"                           //  应用的运行模式
	beego.BConfig.Listen.HTTPPort = HTTPPort_int            //监听端口  本地：8518  线上：8528
	beego.BConfig.WebConfig.AutoRender = false
	go Nats.ReconnectNATS()
	beego.Run()

}
