main.go 1.4 KB

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