main.go 3.4 KB

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