router.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package routers
  2. import (
  3. "mtp2_if/controllers/cptrade"
  4. "mtp2_if/controllers/delivery"
  5. "mtp2_if/controllers/erms2"
  6. "mtp2_if/controllers/wrtrade"
  7. "mtp2_if/logger"
  8. "mtp2_if/token"
  9. "net/http"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. // Swagger生成的文档
  13. _ "mtp2_if/docs"
  14. ginSwagger "github.com/swaggo/gin-swagger"
  15. "github.com/swaggo/gin-swagger/swaggerFiles"
  16. )
  17. // InitRouter 初始化路由器的方法
  18. func InitRouter() *gin.Engine {
  19. r := gin.New()
  20. // 设置日志中间件
  21. r.Use(ginLoggerMiddleware())
  22. // 设置奔溃中间件
  23. r.Use(ginRecoveryMiddleware())
  24. // 跨域
  25. r.Use(CORSMiddleware())
  26. // Swagger
  27. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  28. // 路由分组
  29. apiR := r.Group("/api")
  30. apiR.Use(token.Auth())
  31. // ************************ 仓单贸易 ************************
  32. wrTradeR := apiR.Group("WRTrade")
  33. wrTradeR.Use()
  34. {
  35. wrTradeR.GET("/GetAllDeliveryGoods", wrtrade.GetAllDeliveryGoods)
  36. }
  37. // ************************ 产能预售 ************************
  38. cpTradeR := apiR.Group("CPTrade")
  39. cpTradeR.Use()
  40. {
  41. // 查询产能预售申请表
  42. cpTradeR.GET("/QueryPreasleApply", cptrade.QueryPreasleApply)
  43. // 查询远期订单信息
  44. cpTradeR.GET("/QueryUserGoodsData", cptrade.QueryUserGoodsData)
  45. // 查询远期订单注销申请信息
  46. cpTradeR.GET("/QueryPositionCancel", cptrade.QueryPositionCancel)
  47. // 查询产能预售商品扩展信息
  48. cpTradeR.GET("/QueryPresaleGoodsEx", cptrade.QueryPresaleGoodsEx)
  49. // 查询产能预售我的出价信息
  50. cpTradeR.GET("/QueryCPTradeMyBidInfos", cptrade.QueryCPTradeMyBidInfos)
  51. // 查询我的预售信息
  52. cpTradeR.GET("/QueryMyCPTradeGoods", cptrade.QueryMyCPTradeGoods)
  53. // 查询产能预售委托单信息
  54. cpTradeR.GET("/QueryCPTradeOrderDetail", cptrade.QueryCPTradeOrderDetail)
  55. }
  56. // ************************ 交割服务 ************************
  57. deliveryR := apiR.Group("Delivery")
  58. deliveryR.Use()
  59. {
  60. // 查询商品交割关系表
  61. deliveryR.GET("/QueryDeliveryRelation", delivery.QueryDeliveryRelation)
  62. }
  63. // ************************ 风险管理 ************************
  64. erms2R := apiR.Group("Erms2")
  65. erms2R.Use()
  66. {
  67. // 查询内部成交单信息
  68. erms2R.GET("/QueryInnerTradeDetail", erms2.QueryInnerTradeDetail)
  69. }
  70. return r
  71. }
  72. func ginLoggerMiddleware() gin.HandlerFunc {
  73. return func(c *gin.Context) {
  74. start := time.Now()
  75. c.Next()
  76. end := time.Now()
  77. latency := end.Sub(start)
  78. path := c.Request.URL.RequestURI()
  79. clientip := c.ClientIP()
  80. method := c.Request.Method
  81. statuscode := c.Writer.Status()
  82. logger.GetLogger().Infof("|%3d|%13v|%15s|%s %s",
  83. statuscode,
  84. latency,
  85. clientip,
  86. method,
  87. path)
  88. }
  89. }
  90. func ginRecoveryMiddleware() gin.HandlerFunc {
  91. return gin.RecoveryWithWriter(logger.GetLogWriter())
  92. }
  93. // CORSMiddleware cors跨域.
  94. func CORSMiddleware() gin.HandlerFunc {
  95. return func(c *gin.Context) {
  96. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  97. c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
  98. c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With, X-session-Token")
  99. c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
  100. c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, X-session-Token")
  101. if c.Request.Method == "OPTIONS" {
  102. c.AbortWithStatus(http.StatusNoContent)
  103. return
  104. }
  105. c.Next()
  106. }
  107. }