main.go 1.9 KB

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