router.go 6.5 KB

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