main.go 3.2 KB

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