main.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "AIOTCOER/Plugin"
  4. "AIOTCOER/conf"
  5. "AIOTCOER/logs"
  6. "AIOTCOER/models/Device"
  7. _ "AIOTCOER/routers"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "github.com/beego/beego/v2/server/web/filter/cors"
  12. _ "github.com/go-sql-driver/mysql"
  13. "runtime"
  14. "strconv"
  15. )
  16. func init() {
  17. runtime.GOMAXPROCS(runtime.NumCPU())
  18. //logs.Println("MysqlServer:" + conf.MysqlServer_Username + ":" + conf.MysqlServer_Password + "@tcp(" + conf.MysqlServer_UrlPort + ")/" + conf.MysqlServer_Database + "?charset=utf8mb4&loc=Local&parseTime=True")
  19. orm.RegisterDriver("mysql", orm.DRMySQL)
  20. orm.RegisterDataBase("default", "mysql",
  21. conf.MysqlServer_Username+":"+conf.MysqlServer_Password+"@tcp("+conf.MysqlServer_UrlPort+")/"+conf.MysqlServer_Database+"?charset=utf8mb4&loc=Local&parseTime=True",
  22. conf.MysqlServer_MaxIdleConnections, conf.MysqlServer_MaxOpenConnections)
  23. orm.RunSyncdb("default", false, true) // 创建数据库
  24. }
  25. func main() {
  26. beego.BConfig.AppName = conf.AppName + "_" + strconv.Itoa(conf.HTTPPort) // 项目名
  27. beego.BConfig.ServerName = conf.AppName + "_" + strconv.Itoa(conf.HTTPPort) //server 名称
  28. beego.BConfig.RunMode = "dev" // 应用的运行模式
  29. beego.BConfig.Listen.HTTPPort = conf.HTTPPort //监听端口 本地:8518 线上:8528
  30. beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
  31. // 允许访问所有源
  32. AllowAllOrigins: true,
  33. // 可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有)
  34. AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
  35. // 指的是允许的Header的种类
  36. AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
  37. // 公开的HTTP标头列表
  38. ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
  39. // 如果设置,则允许共享身份验证凭据,例如cookie
  40. AllowCredentials: true,
  41. }))
  42. orm2.Debug = true
  43. logs.Println("MqttServer", " ======= 项目启动 ========")
  44. go Device.ConnectMongodb() // 连接 Mongodb
  45. go Plugin.PluginInit() // Plugin 初始化
  46. beego.Run()
  47. }