苏州瑞玛APS项目web后台
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.

135 lines
4.6 KiB

3 years ago
3 years ago
3 years ago
  1. package conf
  2. import (
  3. "fmt"
  4. "github.com/kataras/iris/v12"
  5. "github.com/kataras/iris/v12/middleware/logger"
  6. rcover "github.com/kataras/iris/v12/middleware/recover"
  7. "leit.com/LAPP_CHEERSSON_BACKEND/web/supports"
  8. )
  9. var ExampleFile = "http://localhost:9001/public/uploadxlsx/example.xlsx"
  10. var Baseurl = "http://localhost:9003"
  11. type AppConf struct {
  12. DisablePathCorrection bool
  13. EnablePathEscape bool
  14. FireMethodNotAllowed bool
  15. DisableBodyConsumptionOnUnmarshal bool
  16. TimeFormat string
  17. Charset string
  18. Other Other
  19. PublicRute []string
  20. }
  21. type Other struct {
  22. IgnoreURLs []string
  23. JWTTimeout int64
  24. LogLevel string
  25. Secret string
  26. }
  27. var AppConfig AppConf = AppConf{
  28. DisablePathCorrection: false,
  29. EnablePathEscape: false,
  30. FireMethodNotAllowed: false,
  31. DisableBodyConsumptionOnUnmarshal: false,
  32. TimeFormat: "Mon, 01 Jan 2006 15:04:05 GMT",
  33. Charset: "UTF-8",
  34. Other: Other{
  35. IgnoreURLs: []string{"/", "/admin/common/log/read/", "/user/login", "/user/plants", "/admin/tabnames/download", "/user/getimg", "/admin/api/screw/receive", "/admin/log/artdemandhead/selectlastmodify", "/admin/log/artdemandlst/synchronous", "/admin/log/artdemandlstcache/synchronouscache", "/admin/report/filterdatainfo/display", "/admin/ws", "/datal"}, //免token验证路径
  36. JWTTimeout: 72000,
  37. LogLevel: "debug",
  38. Secret: "lapp_gaas_gframe",
  39. },
  40. PublicRute: []string{"/sysMenu", "/admin/common/log/read", "/user/getuserinfo", "/user/pwd", "/user/avatar", "/user/profile", "/user/getimg", "/admin/tabnames/list", "/admin/stdeftab/selectarr", "/admin/stdeftab/selectall", "/admin/log/artdemandhead/selectlastmodify", "/admin/log/artdemandlst/synchronous", "/admin/log/artdemandlstcache/synchronouscache", "/datal"}, //免权限检查路径
  41. }
  42. var (
  43. // conf strut
  44. C iris.Configuration
  45. // 解析app.yml中的Other项
  46. O Other
  47. // app.conf配置项key定义
  48. ignoreURLs string = "IgnoreURLs"
  49. jwtTimeout string = "JWTTimeout"
  50. logLevel string = "LogLevel"
  51. secret string = "Secret"
  52. )
  53. func AppOtherParse() {
  54. appData := AppConfig
  55. c := iris.DefaultConfiguration()
  56. c.DisableBodyConsumptionOnUnmarshal = appData.DisableBodyConsumptionOnUnmarshal
  57. c.DisablePathCorrection = appData.DisablePathCorrection
  58. c.EnablePathEscape = appData.EnablePathEscape
  59. c.FireMethodNotAllowed = appData.FireMethodNotAllowed
  60. c.TimeFormat = appData.TimeFormat
  61. c.Charset = appData.Charset
  62. other := make(map[string]interface{}, 0)
  63. other["IgnoreURLs"] = appData.Other.IgnoreURLs
  64. other["JWTTimeout"] = appData.Other.JWTTimeout
  65. other["LogLevel"] = appData.Other.LogLevel
  66. other["Secret"] = appData.Other.Secret
  67. c.Other = other
  68. C = c
  69. // 解析other的key
  70. iURLs := c.GetOther()[ignoreURLs].([]string)
  71. fmt.Println(iURLs)
  72. for _, v := range iURLs {
  73. O.IgnoreURLs = append(O.IgnoreURLs, v)
  74. }
  75. jTimeout := c.GetOther()[jwtTimeout].(int64)
  76. O.JWTTimeout = int64(jTimeout)
  77. //golog.Info(reflect.TypeOf(O.JWTTimeout))
  78. O.LogLevel = c.GetOther()[logLevel].(string)
  79. O.Secret = c.GetOther()[secret].(string)
  80. }
  81. // 注册中间件、定义错误处理
  82. func PreSettring(app *iris.Application) {
  83. app.Logger().SetLevel(AppConfig.Other.LogLevel)
  84. customLogger := logger.New(logger.Config{
  85. //状态显示状态代码
  86. Status: true,
  87. // IP显示请求的远程地址
  88. IP: true,
  89. //方法显示http方法
  90. Method: true,
  91. // Path显示请求路径
  92. Path: true,
  93. // Query将url查询附加到Path。
  94. Query: true,
  95. //Columns:true,
  96. // 如果不为空然后它的内容来自`ctx.Values(),Get("logger_message")
  97. //将添加到日志中。
  98. MessageContextKeys: []string{"logger_message"},
  99. //如果不为空然后它的内容来自`ctx.GetHeader(“User-Agent”)
  100. MessageHeaderKeys: []string{"User-Agent"},
  101. })
  102. app.Use(
  103. rcover.New(),
  104. customLogger,
  105. //middleware.ServeHTTP
  106. )
  107. // ---------------------- 定义错误处理 ------------------------
  108. app.OnErrorCode(iris.StatusNotFound, customLogger, func(ctx iris.Context) {
  109. supports.Error(ctx, iris.StatusNotFound, supports.NotFound, nil)
  110. })
  111. //app.OnErrorCode(iris.StatusForbidden, customLogger, func(ctx iris.Context) {
  112. // ctx.JSON(utils.Error(iris.StatusForbidden, "权限不足", nil))
  113. //})
  114. //捕获所有http错误:
  115. //app.OnAnyErrorCode(customLogger, func(ctx iris.Context) {
  116. // //这应该被添加到日志中,因为`logger.Config#MessageContextKey`
  117. // ctx.Values().Set("logger_message", "a dynamic message passed to the logs")
  118. // ctx.JSON(utils.Error(500, "服务器内部错误", nil))
  119. //})
  120. }