main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "eta_gn/eta_api/controllers"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/services/alarm_msg"
  6. "fmt"
  7. "runtime"
  8. "time"
  9. _ "eta_gn/eta_api/global"
  10. _ "eta_gn/eta_api/routers"
  11. "eta_gn/eta_api/services"
  12. "eta_gn/eta_api/utils"
  13. "github.com/beego/beego/v2/adapter/logs"
  14. "github.com/beego/beego/v2/server/web"
  15. "github.com/beego/beego/v2/server/web/context"
  16. )
  17. func main() {
  18. if web.BConfig.RunMode == "dev" {
  19. web.BConfig.WebConfig.DirectoryIndex = true
  20. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  21. }
  22. // 服务名称
  23. web.BConfig.ServerName = utils.APPNAME
  24. go services.Task()
  25. // 初始化表数据入库
  26. models.AfterInitTable()
  27. // 异常处理
  28. web.ErrorController(&controllers.ErrorController{})
  29. // 内存调整
  30. web.BConfig.MaxMemory = 1024 * 1024 * 128
  31. web.BConfig.RecoverFunc = Recover
  32. web.Run()
  33. }
  34. func Recover(ctx *context.Context, conf *web.Config) {
  35. if err := recover(); err != nil {
  36. if err == web.ErrAbort {
  37. return
  38. }
  39. if !web.BConfig.RecoverPanic {
  40. panic(err)
  41. }
  42. stack := ""
  43. msg := fmt.Sprintf("The request url is %v", ctx.Input.URL())
  44. stack += msg + "</br>"
  45. logs.Critical(msg)
  46. msg = fmt.Sprintf("The request data is %v", string(ctx.Input.RequestBody))
  47. stack += msg + "</br>"
  48. logs.Critical(msg)
  49. msg = fmt.Sprintf("Handler crashed with error %v", err)
  50. stack += msg + "</br>"
  51. logs.Critical(msg)
  52. for i := 1; ; i++ {
  53. _, file, line, ok := runtime.Caller(i)
  54. if !ok {
  55. break
  56. }
  57. logs.Critical(fmt.Sprintf("%s:%d", file, line))
  58. stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d</br>", file, line))
  59. }
  60. go alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
  61. }
  62. return
  63. }