main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. _ "ERP_storage/Nats"
  4. "ERP_storage/conf"
  5. "ERP_storage/controllers"
  6. "ERP_storage/logs"
  7. _ "ERP_storage/models"
  8. _ "ERP_storage/models/Account"
  9. "ERP_storage/models/Basic"
  10. _ "ERP_storage/models/Contract"
  11. _ "ERP_storage/models/Property"
  12. _ "ERP_storage/models/Stock"
  13. _ "ERP_storage/routers"
  14. "fmt"
  15. "github.com/beego/beego/v2/adapter/orm"
  16. orm2 "github.com/beego/beego/v2/client/orm"
  17. beego "github.com/beego/beego/v2/server/web"
  18. "github.com/beego/beego/v2/server/web/context"
  19. "github.com/beego/beego/v2/server/web/filter/cors"
  20. _ "github.com/go-sql-driver/mysql"
  21. "runtime"
  22. "strconv"
  23. )
  24. func init() {
  25. fmt.Println(runtime.GOOS)
  26. orm.RegisterDriver("mysql", orm.DRMySQL)
  27. //orm.RegisterDataBase("default", "mysql", "zdxq:7e5853d9178edfcc@tcp(47.108.133.234:3306)/zdxq?charset=utf8",100,200)
  28. orm.RegisterDataBase("default", "mysql",
  29. conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
  30. conf.MysqlServer_MaxIdleConnections, conf.MysqlServer_MaxOpenConnections)
  31. orm.RunSyncdb("default", false, false) // 创建数据库
  32. //Mqtt
  33. orm.RegisterDriver("mysql", orm.DRMySQL)
  34. orm.RegisterDataBase("mqtt", "mysql",
  35. conf.MysqlMqtt_Username+":"+conf.MysqlMqtt_Password+"@tcp("+conf.MysqlMqtt_UrlPort+")/"+conf.MysqlMqtt_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
  36. conf.MysqlMqtt_MaxIdleConnections, conf.MysqlMqtt_MaxOpenConnections)
  37. orm2.Debug = true
  38. //orm.RunSyncdb("mqtt", false, false) // 创建数据库
  39. }
  40. func main() {
  41. HTTPPort, _ := beego.AppConfig.String("HTTPPort")
  42. HTTPPort_int, _ := strconv.Atoi(HTTPPort)
  43. beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
  44. // 允许访问所有源
  45. AllowAllOrigins: true,
  46. // 可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有)
  47. AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
  48. // 指的是允许的Header的种类
  49. AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
  50. // 公开的HTTP标头列表
  51. ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
  52. // 如果设置,则允许共享身份验证凭据,例如cookie
  53. AllowCredentials: true,
  54. }))
  55. appname, _ := beego.AppConfig.String("appname")
  56. beego.BConfig.AppName = appname // 项目名
  57. beego.BConfig.ServerName = appname //server 名称
  58. beego.BConfig.RunMode = "dev" // 应用的运行模式
  59. beego.BConfig.Listen.HTTPPort = HTTPPort_int //监听端口 本地:8518 线上:8528
  60. beego.BConfig.WebConfig.AutoRender = false
  61. beego.BConfig.RecoverPanic = true
  62. beego.BConfig.RecoverFunc = RecoverPanic
  63. go Basic.Read_ProductClass_All_Map() // 初始化产品类型
  64. go controllers.Cron_StockMonth() // 库存明细详情定时任务
  65. go controllers.Cron_VerifyContract() // 验证合同状态修改定时任务
  66. //go controllers.Cron_Device_Add() // 重写设备表 只执行一次
  67. go controllers.CheckPowerUniformity() // 检查角色是否与用户系统一致,不存在则添加
  68. //go controllers.Cron_StockMonth_Add() // 统计库存明细,测试使用
  69. beego.Run()
  70. }
  71. func RecoverPanic(ctx *context.Context, config *beego.Config) {
  72. if err := recover(); err != nil {
  73. ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", ctx.Request.Header.Get("Origin"))
  74. var stack []string
  75. for i := 1; ; i++ {
  76. _, file, line, ok := runtime.Caller(i)
  77. if !ok {
  78. break
  79. }
  80. logs.Error(fmt.Sprintf("%s:%d", file, line))
  81. stack = append(stack, fmt.Sprintln(fmt.Sprintf("%s:%d", file, line)))
  82. }
  83. //显示错误
  84. data := map[string]interface{}{
  85. "AppError": fmt.Sprintf("%v", err),
  86. "Code": 500,
  87. "Msg": "请稍后重试!",
  88. }
  89. if conf.RunMode == "dev" {
  90. data["RequestMethod"] = ctx.Input.Method()
  91. data["RequestURL"] = ctx.Input.URI()
  92. data["RemoteAddr"] = ctx.Input.IP()
  93. data["Stack"] = stack
  94. data["GoVersion"] = runtime.Version()
  95. }
  96. _ = ctx.Output.JSON(data, true, true)
  97. //if ctx.Output.Status != 0 {
  98. // ctx.ResponseWriter.WriteHeader(ctx.Output.Status)
  99. //} else {
  100. // ctx.ResponseWriter.WriteHeader(500)
  101. //}
  102. }
  103. }