router.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package routers
  2. import (
  3. "mtp2_if/controllers/cfg"
  4. "mtp2_if/controllers/common"
  5. "mtp2_if/controllers/cptrade"
  6. "mtp2_if/controllers/delivery"
  7. "mtp2_if/controllers/erms2"
  8. "mtp2_if/controllers/order"
  9. "mtp2_if/controllers/szdz"
  10. "mtp2_if/controllers/user"
  11. "mtp2_if/controllers/wrtrade"
  12. "mtp2_if/logger"
  13. "mtp2_if/token"
  14. "net/http"
  15. "time"
  16. "github.com/gin-gonic/gin"
  17. // Swagger生成的文档
  18. _ "mtp2_if/docs"
  19. ginSwagger "github.com/swaggo/gin-swagger"
  20. "github.com/swaggo/gin-swagger/swaggerFiles"
  21. )
  22. // InitRouter 初始化路由器的方法
  23. func InitRouter() *gin.Engine {
  24. r := gin.New()
  25. // 设置日志中间件
  26. r.Use(ginLoggerMiddleware())
  27. // 设置奔溃中间件
  28. r.Use(ginRecoveryMiddleware())
  29. // 跨域
  30. r.Use(CORSMiddleware())
  31. // Swagger
  32. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  33. // 终端配置
  34. r.GET("/cfg", cfg.QueryCfg)
  35. // 主业务路由分组
  36. apiR := r.Group("/api")
  37. // apiR.Use(token.Auth())
  38. // ************************ 账户信息 ************************
  39. userR := apiR.Group("User")
  40. userR.Use()
  41. {
  42. // 获取登录ID
  43. userR.GET("/GetLoginID", user.GetLoginID)
  44. // 获取用户邀请码请求参数
  45. userR.GET("/QueryUserReferNum", user.QueryUserReferNum)
  46. }
  47. // ************************ 通用服务 ************************
  48. commonR := apiR.Group("Common")
  49. commonR.Use()
  50. {
  51. // 查询交易端菜单
  52. commonR.GET("/QueryTraderMenu", common.QueryTraderMenu)
  53. // 查询交易端列表头信息
  54. commonR.GET("/QueryTableDefine", common.QueryTableDefine)
  55. }
  56. // ************************ 通用单据 ************************
  57. orderR := apiR.Group("Order")
  58. orderR.Use(token.Auth())
  59. {
  60. // 持仓汇总查询(合约市场)
  61. orderR.GET("/QueryTradePosition", order.QueryTradePosition)
  62. // 委托单查询请求(合约市场)
  63. orderR.GET("/QueryTradeOrderDetail", order.QueryTradeOrderDetail)
  64. }
  65. // ************************ 仓单贸易 ************************
  66. wrTradeR := apiR.Group("WRTrade")
  67. wrTradeR.Use(token.Auth())
  68. {
  69. wrTradeR.GET("/GetAllDeliveryGoods", wrtrade.GetAllDeliveryGoods)
  70. }
  71. // ************************ 产能预售 ************************
  72. cpTradeR := apiR.Group("CPTrade")
  73. cpTradeR.Use(token.Auth())
  74. {
  75. // 查询产能预售申请表
  76. cpTradeR.GET("/QueryPreasleApply", cptrade.QueryPreasleApply)
  77. // 查询远期订单信息
  78. cpTradeR.GET("/QueryUserGoodsData", cptrade.QueryUserGoodsData)
  79. // 查询远期订单注销申请信息
  80. cpTradeR.GET("/QueryPositionCancel", cptrade.QueryPositionCancel)
  81. // 查询产能预售商品扩展信息
  82. cpTradeR.GET("/QueryPresaleGoodsEx", cptrade.QueryPresaleGoodsEx)
  83. // 查询产能预售我的出价信息
  84. cpTradeR.GET("/QueryCPTradeMyBidInfos", cptrade.QueryCPTradeMyBidInfos)
  85. // 查询我的预售信息
  86. cpTradeR.GET("/QueryMyCPTradeGoods", cptrade.QueryMyCPTradeGoods)
  87. // 查询产能预售委托单信息
  88. cpTradeR.GET("/QueryCPTradeOrderDetail", cptrade.QueryCPTradeOrderDetail)
  89. }
  90. // ************************ 交割服务 ************************
  91. deliveryR := apiR.Group("Delivery")
  92. deliveryR.Use(token.Auth())
  93. {
  94. // 查询商品交割关系表
  95. deliveryR.GET("/QueryDeliveryRelation", delivery.QueryDeliveryRelation)
  96. }
  97. // ************************ 风险管理 ************************
  98. erms2R := apiR.Group("Erms2")
  99. erms2R.Use(token.Auth())
  100. {
  101. // 查询内部成交单信息
  102. erms2R.GET("/QueryInnerTradeDetail", erms2.QueryInnerTradeDetail)
  103. // 查询期现套利策略表信息(指定资金账户、未结束的)
  104. erms2R.GET("/QueryArbitrageStrategy", erms2.QueryArbitrageStrategy)
  105. // 查询现货合同表信息(指定策略ID、未结束的)
  106. erms2R.GET("/QuerySpotContract", erms2.QuerySpotContract)
  107. }
  108. // ************************ 尚志大宗 ************************
  109. szdzR := apiR.Group("SZDZ")
  110. szdzR.Use(token.Auth())
  111. {
  112. // 点选挂牌委托单据查询(摘牌大厅)
  113. szdzR.GET("/QueryRecieptOrder", szdz.QueryRecieptOrder)
  114. // 商品提货单查询
  115. szdzR.GET("/QueryGoodsPickup", szdz.QueryGoodsPickup)
  116. // 交易系统转换流水查询
  117. szdzR.GET("/QueryConvertLog", szdz.QueryConvertLog)
  118. }
  119. return r
  120. }
  121. func ginLoggerMiddleware() gin.HandlerFunc {
  122. return func(c *gin.Context) {
  123. start := time.Now()
  124. c.Next()
  125. end := time.Now()
  126. latency := end.Sub(start)
  127. path := c.Request.URL.RequestURI()
  128. clientip := c.ClientIP()
  129. method := c.Request.Method
  130. statuscode := c.Writer.Status()
  131. logger.GetLogger().Infof("|%3d|%13v|%15s|%s %s",
  132. statuscode,
  133. latency,
  134. clientip,
  135. method,
  136. path)
  137. }
  138. }
  139. func ginRecoveryMiddleware() gin.HandlerFunc {
  140. return gin.RecoveryWithWriter(logger.GetLogWriter())
  141. }
  142. // CORSMiddleware cors跨域.
  143. func CORSMiddleware() gin.HandlerFunc {
  144. return func(c *gin.Context) {
  145. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  146. c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
  147. 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")
  148. c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
  149. c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, X-session-Token")
  150. if c.Request.Method == "OPTIONS" {
  151. c.AbortWithStatus(http.StatusNoContent)
  152. return
  153. }
  154. c.Next()
  155. }
  156. }