GAAS GFrame项目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.

139 lines
4.7 KiB

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