package main

import (
	"context"

	"ipsomc/core/dao/mysql/mysqlapi"
	"ipsomc/core/dao/redis/redisapi"
	"ipsomc/core/router"
	"ipsomc/module/socket/socketcreate"
	"ipsomc/module/timer"
	"ipsomc/public"
	"ipsomc/util"

	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

// socket模块
var socketCreateApi socketcreate.SocketCreate

// 系统工作模式，0：开发模式，1：生产模式
var sysWorkMode uint32 = 1

func main() {
	ctx := context.Background()

	//设置系统工作模式
	public.PublicSetSysWorkMode(sysWorkMode)

	//初始化配置文件
	util.ViperConfigInit("config", "config")

	//创建定时器
	go timer.CreateTimer(ctx)

	//创建socket监听
	//go socketCreateApi.CreateUdpSocket()
	go socketCreateApi.CreateTcpSocket()

	//打开redis数据库
	redisapi.OpenRds(ctx, "redisDefault")
	defer redisapi.CloseRds()

	//打开MySql数据库
	mysqlapi.OpenSqlDb(sysWorkMode)
	defer mysqlapi.CloseSqlDb()

	//注册并启动路由
	runMode := gin.ReleaseMode //发布模式，
	if sysWorkMode == 0 {
		runMode = gin.DebugMode //调试模式
	}
	r := router.SetRouter(runMode, sysWorkMode)
	r.Run(":8080") //监听
}
