main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/beego/beego/v2/server/web"
  6. "github.com/beego/beego/v2/server/web/context"
  7. _ "hongze/hongze_mfyx_gzh/routers"
  8. "hongze/hongze_mfyx_gzh/utils"
  9. "runtime"
  10. "time"
  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. }