web.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package commands
  2. import (
  3. "crm-api/internal/app"
  4. "crm-api/pkg/configs"
  5. "crm-api/pkg/lfshook"
  6. "crm-api/pkg/utils"
  7. "fmt"
  8. "io/ioutil"
  9. // _ "pms-api-go/api/admin/swagger" // 20230419 pms 注释
  10. "github.com/gin-gonic/gin"
  11. "github.com/sirupsen/logrus"
  12. "github.com/urfave/cli"
  13. "gopkg.in/natefinch/lumberjack.v2"
  14. )
  15. // CmdWeb cli 命令
  16. var CmdWeb = cli.Command{
  17. Name: "web",
  18. Usage: "./pbx-api-gin web",
  19. Description: "Run PBX Web Server",
  20. Action: runWeb,
  21. Flags: []cli.Flag{
  22. cli.StringFlag{Name: "config, c", Usage: "config file path"},
  23. },
  24. }
  25. func runWeb(ctx *cli.Context) error {
  26. if !ctx.IsSet("config") {
  27. fmt.Print("not set config file path, add -config param")
  28. return fmt.Errorf("not set config")
  29. }
  30. // 解析配置文件
  31. configs.ConfigPath = ctx.String("config")
  32. configs.DecodeConfig()
  33. configs.GetPushConfig()
  34. if ctx.GlobalIsSet("level") {
  35. configs.ConfigGlobal.LogLevel = logrus.Level(ctx.GlobalInt("level"))
  36. } else {
  37. configs.ConfigGlobal.LogLevel = logrus.InfoLevel
  38. }
  39. if ctx.GlobalBool("release") {
  40. gin.SetMode(gin.ReleaseMode)
  41. } else {
  42. gin.SetMode(gin.DebugMode)
  43. }
  44. if ctx.GlobalBool("reportCaller") {
  45. lfshook.NewLogger().Logger.SetReportCaller(true)
  46. }
  47. if ctx.GlobalBool("disableConsoleLog") {
  48. lfshook.NewLogger().Logger.Out = ioutil.Discard
  49. }
  50. lfshook.NewLogger().Logger.SetFormatter(&logrus.TextFormatter{
  51. ForceQuote: false,
  52. FullTimestamp: true,
  53. TimestampFormat: "01-02 15:04:05",
  54. })
  55. pathMap := lfshook.LoggerMap{
  56. logrus.InfoLevel: &lumberjack.Logger{
  57. Filename: configs.ConfigGlobal.LogInfoPath,
  58. MaxSize: 10, // maxSize M
  59. MaxBackups: 5, // keep 5 file
  60. MaxAge: 7, // 7 day
  61. },
  62. logrus.WarnLevel: &lumberjack.Logger{
  63. Filename: configs.ConfigGlobal.LogErrorPath,
  64. MaxSize: 10, // maxSize M
  65. MaxBackups: 5, // keep 5 file
  66. MaxAge: 7, // 7 day
  67. },
  68. logrus.ErrorLevel: &lumberjack.Logger{
  69. Filename: configs.ConfigGlobal.LogErrorPath,
  70. MaxSize: 10, // maxSize M
  71. MaxBackups: 5, // keep 5 file
  72. MaxAge: 7, // 7 day
  73. },
  74. logrus.FatalLevel: &lumberjack.Logger{
  75. Filename: configs.ConfigGlobal.LogErrorPath,
  76. MaxSize: 10, // maxSize M
  77. MaxBackups: 5, // keep 5 file
  78. MaxAge: 7, // 7 day
  79. },
  80. logrus.PanicLevel: &lumberjack.Logger{
  81. Filename: configs.ConfigGlobal.LogErrorPath,
  82. MaxSize: 10, // maxSize M
  83. MaxBackups: 5, // keep 5 file
  84. MaxAge: 7, // 7 day
  85. },
  86. }
  87. lfshook.NewLogger().Logger.Hooks.Add(lfshook.NewHook(
  88. pathMap,
  89. &logrus.TextFormatter{
  90. DisableColors: true,
  91. ForceQuote: true,
  92. TimestampFormat: "01-02 15:04:05",
  93. },
  94. ))
  95. app.StartApp()
  96. utils.Exit()
  97. return nil
  98. }