main.go 3.2 KB

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