main.go 1.3 KB

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