package main import ( "context" "designs/bootstrap" "designs/global" "designs/utils" "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(Cors()) // 初始化Redis global.Init.InitRedisFunc = bootstrap.InitializeRedis global.App.Redis = bootstrap.InitializeRedis() // 初始化facade global.InitFacade() //服务器端口 //ginServer.Run(":" + config.Get("app.port")) /*默认是8080*/ // 初始化数据库 global.App.DB = &utils.WtDB{DB: bootstrap.InitializeDB()} // 程序关闭前,释放数据库连接 defer func() { if global.App.DB != nil { db, _ := global.App.DB.DB.DB() db.Close() } }() //初始化mongodb global.App.MongoDB = bootstrap.InitializeMongo() defer func() { if global.App.MongoDB != nil { global.App.MongoDB.Disconnect(context.Background()) } }() bootstrap.InitializeCron() 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) } } }