main.go 1.6 KB

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