main.go 1.5 KB

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