main.go 1.6 KB

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