main.go 1.6 KB

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