main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "eta_gn/eta_chart_lib/controllers"
  4. "eta_gn/eta_chart_lib/models"
  5. _ "eta_gn/eta_chart_lib/routers"
  6. "eta_gn/eta_chart_lib/services/alarm_msg"
  7. "eta_gn/eta_chart_lib/utils"
  8. "fmt"
  9. "runtime"
  10. "time"
  11. "github.com/beego/beego/v2/adapter/logs"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "github.com/beego/beego/v2/server/web/context"
  14. )
  15. func main() {
  16. if beego.BConfig.RunMode == "dev" {
  17. beego.BConfig.WebConfig.DirectoryIndex = true
  18. beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  19. }
  20. models.AfterInitTable()
  21. beego.ErrorController(&controllers.ErrorController{})
  22. beego.BConfig.RecoverFunc = Recover
  23. beego.Run()
  24. }
  25. func Recover(ctx *context.Context, conf *beego.Config) {
  26. if err := recover(); err != nil {
  27. if err == beego.ErrAbort {
  28. return
  29. }
  30. if !beego.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 alarm_msg.SendAlarmMsg(utils.APPNAME+"崩了"+time.Now().Format("2006-01-02 15:04:05")+"<br/>"+stack, 3)
  52. }
  53. return
  54. }