main.go 1.6 KB

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