main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. _ "Cold_Api/Nats"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers"
  6. _ "Cold_Api/initialize"
  7. "Cold_Api/models/Device"
  8. "Cold_Api/models/Product"
  9. "Cold_Api/models/Warning"
  10. "Cold_Api/routers"
  11. _ "Cold_Api/routers"
  12. "fmt"
  13. "github.com/astaxie/beego/logs"
  14. "github.com/beego/beego/v2/adapter/orm"
  15. orm2 "github.com/beego/beego/v2/client/orm"
  16. beego "github.com/beego/beego/v2/server/web"
  17. "github.com/beego/beego/v2/server/web/context"
  18. "github.com/beego/beego/v2/server/web/filter/cors"
  19. _ "github.com/go-sql-driver/mysql"
  20. "runtime"
  21. "strconv"
  22. )
  23. func init() {
  24. //lib.WritePid()
  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. println(conf.MysqlServer_Username + ":" + conf.MysqlServer_Password + "@tcp(" + conf.MysqlServer_UrlPort + ")/" + conf.MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True")
  33. orm2.Debug = conf.MysqlServer_Debug
  34. // sql文件输出到日志
  35. if orm2.Debug == true {
  36. sqllog := logs.NewLogger()
  37. //sqllog.SetLogger(logs.AdapterFile, `{"filename":"logs/sql/sql.log","level":7,"maxlines":10000,"maxsize":1024000,"daily":true,"maxdays":10,"color":true}`)
  38. sqllog.SetLogger(logs.AdapterFile, `{"filename":"logs/sql/sql.log"}`)
  39. if conf.RunMode == "dev" {
  40. sqllog.SetLogger(logs.AdapterConsole)
  41. }
  42. orm2.DebugLog = orm2.NewLog(sqllog)
  43. }
  44. }
  45. func main() {
  46. HTTPPort_int, _ := strconv.Atoi(conf.HTTPPort)
  47. beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
  48. // 允许访问所有源
  49. AllowAllOrigins: true,
  50. // 可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有)
  51. AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
  52. // 指的是允许的Header的种类
  53. AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
  54. // 公开的HTTP标头列表
  55. ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Access-Control-Allow-Headers", "Content-Type"},
  56. // 如果设置,则允许共享身份验证凭据,例如cookie
  57. AllowCredentials: true,
  58. }))
  59. //过滤器,拦截所有请求
  60. beego.InsertFilter("/*", beego.BeforeRouter, routers.RBACFilter)
  61. beego.BConfig.AppName = conf.AppName // 项目名
  62. beego.BConfig.ServerName = conf.AppName + conf.HTTPPort //server 名称
  63. beego.BConfig.RunMode = conf.RunMode // 应用的运行模式
  64. beego.BConfig.Listen.HTTPPort = HTTPPort_int //监听端口 本地:8518 线上:8528
  65. beego.BConfig.WebConfig.AutoRender = false
  66. beego.BConfig.RecoverPanic = true
  67. beego.BConfig.RecoverFunc = RecoverPanic
  68. go controllers.DeviceRealTime() // 实时 数据
  69. //go controllers.Update_DeviceSensor_Parameter() // 更新传感器表-传感器参数t_sp
  70. go Warning.Read_WarningType_All_Maps() // 初始化报警类型
  71. go Product.Read_ProductType_All_Map() // 初始化产品类型
  72. go Device.Read_DeviceSensorTypeAll_Map() // 初始化传感器类型
  73. if conf.MqttServer_id == "mqttjxit" {
  74. go controllers.Cron_WarningRate() // 只在一台机启动定时任务
  75. }
  76. fmt.Println("======= beego.Run ======")
  77. beego.Run()
  78. }
  79. func RecoverPanic(ctx *context.Context, config *beego.Config) {
  80. if err := recover(); err != nil {
  81. ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", ctx.Request.Header.Get("Origin"))
  82. var stack []string
  83. for i := 1; ; i++ {
  84. _, file, line, ok := runtime.Caller(i)
  85. if !ok {
  86. break
  87. }
  88. logs.Error(fmt.Sprintf("%s:%d", file, line))
  89. stack = append(stack, fmt.Sprintln(fmt.Sprintf("%s:%d", file, line)))
  90. }
  91. //显示错误
  92. data := map[string]interface{}{
  93. "AppError": fmt.Sprintf("%v", err),
  94. "Code": 500,
  95. "Msg": "请稍后重试!",
  96. }
  97. if conf.RunMode == "dev" {
  98. data["RequestMethod"] = ctx.Input.Method()
  99. data["RequestURL"] = ctx.Input.URI()
  100. data["RemoteAddr"] = ctx.Input.IP()
  101. data["Stack"] = stack
  102. data["GoVersion"] = runtime.Version()
  103. }
  104. _ = ctx.Output.JSON(data, true, true)
  105. //if ctx.Output.Status != 0 {
  106. // ctx.ResponseWriter.WriteHeader(ctx.Output.Status)
  107. //} else {
  108. // ctx.ResponseWriter.WriteHeader(500)
  109. //}
  110. }
  111. }