main.go 3.1 KB

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