package main

import (
	"designs/bootstrap"
	"designs/global"
	"designs/middleware"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func init() {
	fmt.Printf("初始化")
}

func main() {
	//创建一个服务
	gin.SetMode(gin.ReleaseMode)

	ginServer := gin.Default()

	// 初始化配置
	bootstrap.InitializeConfig(filepath.Join(global.App.PwdPath, ".env"))

	//config.PrintConfig()

	// 初始化日志
	global.App.Log, global.App.LogWriter = bootstrap.InitializeLog()

	ginServer.Use(middleware.Logmiddleware())
	//ip库
	//common.InitIpFile()
	bootstrap.InitIpFile()

	//跨域
	ginServer.Use(Cors())

	// 初始化Redis
	global.Init.InitRedisFunc = bootstrap.InitializeRedis
	global.App.Redis = bootstrap.InitializeRedis()
	bootstrap.InitGameCfg()

	// 初始化facade
	global.InitFacade()
	//服务器端口
	//ginServer.Run(":" + config.Get("app.port")) /*默认是8080*/

	bootstrap.RunServer()
	// ginServer.RunTLS(":443", "your_certificate.crt", "your_private_key.key")
}

/* 跨域 */
func Cors() gin.HandlerFunc {
	return func(context *gin.Context) {
		method := context.Request.Method
		context.Header("Access-Control-Allow-Origin", "*")
		context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token")
		context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
		context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		context.Header("Access-Control-Allow-Credentials", "true")

		if method == "OPTIONS" {
			context.AbortWithStatus(http.StatusNoContent)
		}
	}
}
