You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.2 KiB

3 years ago
  1. package utilities
  2. import (
  3. "LEIT_PM/web/supports"
  4. "encoding/json"
  5. "github.com/kardianos/service"
  6. "github.com/kataras/iris/v12"
  7. "github.com/kataras/iris/v12/middleware/logger"
  8. "log"
  9. "os"
  10. "path/filepath"
  11. "reflect"
  12. "strings"
  13. )
  14. func GetConfigFromJsonFile(filePath string, config interface{}) error {
  15. currentPath, err := os.Executable()
  16. if err != nil {
  17. return err
  18. }
  19. filePtr, err := os.Open(filepath.Join(filepath.Dir(currentPath), filePath))
  20. if err != nil {
  21. return err
  22. }
  23. defer Close(filePtr)
  24. if err := json.NewDecoder(filePtr).Decode(config); err != nil {
  25. return err
  26. }
  27. return nil
  28. }
  29. func LaunchService(svr service.Interface, svrConfig *service.Config) error {
  30. if svr, err := service.New(svr, svrConfig); err != nil {
  31. return err
  32. } else {
  33. if len(os.Args) > 1 {
  34. switch os.Args[1] {
  35. case "install":
  36. {
  37. if err := svr.Install(); err != nil {
  38. return err
  39. } else {
  40. log.Println("服务安装成功!")
  41. return nil
  42. }
  43. }
  44. case "remove":
  45. {
  46. if err := svr.Uninstall(); err != nil {
  47. return err
  48. } else {
  49. log.Println("H务卸载成功!")
  50. return nil
  51. }
  52. }
  53. default:
  54. {
  55. log.Println("命令行参数解析错错误!")
  56. }
  57. }
  58. return nil
  59. }
  60. if err = svr.Run(); err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. }
  66. func Shrink(sp interface{}) {
  67. pointerOfType := reflect.TypeOf(sp)
  68. // 判断sp是否为指针
  69. if reflect.TypeOf(sp).Kind() != reflect.Ptr {
  70. log.Printf("sp must be a pointer")
  71. return
  72. }
  73. structOfType := pointerOfType.Elem()
  74. // 判断sp是否指向结构体
  75. if structOfType.Kind() != reflect.Struct {
  76. log.Printf("sp must be a pointer of struct")
  77. return
  78. }
  79. value := reflect.ValueOf(sp)
  80. for i := 0; i < structOfType.NumField(); i++ {
  81. if structOfType.Field(i).Type.Name() == "string" {
  82. field := value.Elem().Field(i)
  83. field.Set(reflect.ValueOf(strings.TrimSpace(field.String())))
  84. }
  85. }
  86. return
  87. }
  88. // 注册中间件、定义错误处理
  89. func PreSettring(app *iris.Application) {
  90. app.Logger().SetLevel("debug")
  91. customLogger := logger.New(logger.Config{
  92. //状态显示状态代码
  93. Status: true,
  94. // IP显示请求的远程地址
  95. IP: true,
  96. //方法显示http方法
  97. Method: true,
  98. // Path显示请求路径
  99. Path: true,
  100. // Query将url查询附加到Path。
  101. Query: true,
  102. //Columns:true,
  103. // 如果不为空然后它的内容来自`ctx.Values(),Get("logger_message")
  104. //将添加到日志中。
  105. MessageContextKeys: []string{"logger_message"},
  106. //如果不为空然后它的内容来自`ctx.GetHeader(“User-Agent”)
  107. MessageHeaderKeys: []string{"User-Agent"},
  108. })
  109. app.Use(
  110. customLogger,
  111. )
  112. app.OnErrorCode(iris.StatusNotFound, customLogger, func(ctx iris.Context) {
  113. supports.Error(ctx, iris.StatusNotFound, supports.NotFound, nil)
  114. })
  115. //app.OnErrorCode(iris.StatusForbidden, customLogger, func(ctx iris.Context) {
  116. // ctx.JSON(utils.Error(iris.StatusForbidden, "权限不足", nil))
  117. //})
  118. //捕获所有http错误:
  119. //app.OnAnyErrorCode(customLogger, func(ctx iris.Context) {
  120. // //这应该被添加到日志中,因为`logger.Config#MessageContextKey`
  121. // ctx.Values().Set("logger_message", "a dynamic message passed to the logs")
  122. // ctx.JSON(utils.Error(500, "服务器内部错误", nil))
  123. //})
  124. }