main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "context"
  4. "designs/bootstrap"
  5. "designs/global"
  6. "designs/middleware"
  7. "designs/utils"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "net/http"
  11. "path/filepath"
  12. )
  13. func init() {
  14. fmt.Printf("初始化")
  15. }
  16. func main() {
  17. //创建一个服务
  18. gin.SetMode(gin.ReleaseMode)
  19. ginServer := gin.Default()
  20. // 初始化配置
  21. bootstrap.InitializeConfig(filepath.Join(global.App.PwdPath, ".env"))
  22. //config.PrintConfig()
  23. // 初始化日志
  24. global.App.Log, global.App.LogWriter = bootstrap.InitializeLog()
  25. ginServer.Use(middleware.Logmiddleware())
  26. //ip库
  27. //common.InitIpFile()
  28. bootstrap.InitIpFile()
  29. //跨域
  30. ginServer.Use(Cors())
  31. // 初始化Redis
  32. global.Init.InitRedisFunc = bootstrap.InitializeRedis
  33. global.App.Redis = bootstrap.InitializeRedis()
  34. bootstrap.InitGameCfg()
  35. // 初始化facade
  36. global.InitFacade()
  37. //服务器端口
  38. //ginServer.Run(":" + config.Get("app.port")) /*默认是8080*/
  39. // 初始化数据库
  40. global.App.DB = &utils.WtDB{DB: bootstrap.InitializeDB()}
  41. // 程序关闭前,释放数据库连接
  42. defer func() {
  43. if global.App.DB != nil {
  44. db, _ := global.App.DB.DB.DB()
  45. db.Close()
  46. }
  47. }()
  48. //初始化mongodb
  49. global.App.MongoDB = bootstrap.InitializeMongo()
  50. defer func() {
  51. if global.App.MongoDB != nil {
  52. global.App.MongoDB.Disconnect(context.Background())
  53. }
  54. }()
  55. bootstrap.RunServer()
  56. // ginServer.RunTLS(":443", "your_certificate.crt", "your_private_key.key")
  57. }
  58. /* 跨域 */
  59. func Cors() gin.HandlerFunc {
  60. return func(context *gin.Context) {
  61. method := context.Request.Method
  62. context.Header("Access-Control-Allow-Origin", "*")
  63. context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token")
  64. context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
  65. context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
  66. context.Header("Access-Control-Allow-Credentials", "true")
  67. if method == "OPTIONS" {
  68. context.AbortWithStatus(http.StatusNoContent)
  69. }
  70. }
  71. }