123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package main
- import (
- "context"
- "designs/bootstrap"
- "designs/global"
- "designs/middleware"
- "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(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*/
- // 初始化数据库
- 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.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)
- }
- }
- }
|