main.go 2.0 KB

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