main.go 1.6 KB

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