router.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // ************************ 仓单贸易 ************************
  64. wrTradeR := apiR.Group("WRTrade")
  65. wrTradeR.Use(token.Auth())
  66. {
  67. wrTradeR.GET("/GetAllDeliveryGoods", wrtrade.GetAllDeliveryGoods)
  68. }
  69. // ************************ 产能预售 ************************
  70. cpTradeR := apiR.Group("CPTrade")
  71. cpTradeR.Use(token.Auth())
  72. {
  73. // 查询产能预售申请表
  74. cpTradeR.GET("/QueryPreasleApply", cptrade.QueryPreasleApply)
  75. // 查询远期订单信息
  76. cpTradeR.GET("/QueryUserGoodsData", cptrade.QueryUserGoodsData)
  77. // 查询远期订单注销申请信息
  78. cpTradeR.GET("/QueryPositionCancel", cptrade.QueryPositionCancel)
  79. // 查询产能预售商品扩展信息
  80. cpTradeR.GET("/QueryPresaleGoodsEx", cptrade.QueryPresaleGoodsEx)
  81. // 查询产能预售我的出价信息
  82. cpTradeR.GET("/QueryCPTradeMyBidInfos", cptrade.QueryCPTradeMyBidInfos)
  83. // 查询我的预售信息
  84. cpTradeR.GET("/QueryMyCPTradeGoods", cptrade.QueryMyCPTradeGoods)
  85. // 查询产能预售委托单信息
  86. cpTradeR.GET("/QueryCPTradeOrderDetail", cptrade.QueryCPTradeOrderDetail)
  87. }
  88. // ************************ 交割服务 ************************
  89. deliveryR := apiR.Group("Delivery")
  90. deliveryR.Use(token.Auth())
  91. {
  92. // 查询商品交割关系表
  93. deliveryR.GET("/QueryDeliveryRelation", delivery.QueryDeliveryRelation)
  94. }
  95. // ************************ 风险管理 ************************
  96. erms2R := apiR.Group("Erms2")
  97. erms2R.Use(token.Auth())
  98. {
  99. // 查询内部成交单信息
  100. erms2R.GET("/QueryInnerTradeDetail", erms2.QueryInnerTradeDetail)
  101. // 查询期现套利策略表信息(指定资金账户、未结束的)
  102. erms2R.GET("/QueryArbitrageStrategy", erms2.QueryArbitrageStrategy)
  103. // 查询现货合同表信息(指定策略ID、未结束的)
  104. erms2R.GET("/QuerySpotContract", erms2.QuerySpotContract)
  105. }
  106. // ************************ 尚志大宗 ************************
  107. szdzR := apiR.Group("SZDZ")
  108. szdzR.Use(token.Auth())
  109. {
  110. // 点选挂牌委托单据查询(摘牌大厅)
  111. szdzR.GET("/QueryRecieptOrder", szdz.QueryRecieptOrder)
  112. }
  113. return r
  114. }
  115. func ginLoggerMiddleware() gin.HandlerFunc {
  116. return func(c *gin.Context) {
  117. start := time.Now()
  118. c.Next()
  119. end := time.Now()
  120. latency := end.Sub(start)
  121. path := c.Request.URL.RequestURI()
  122. clientip := c.ClientIP()
  123. method := c.Request.Method
  124. statuscode := c.Writer.Status()
  125. logger.GetLogger().Infof("|%3d|%13v|%15s|%s %s",
  126. statuscode,
  127. latency,
  128. clientip,
  129. method,
  130. path)
  131. }
  132. }
  133. func ginRecoveryMiddleware() gin.HandlerFunc {
  134. return gin.RecoveryWithWriter(logger.GetLogWriter())
  135. }
  136. // CORSMiddleware cors跨域.
  137. func CORSMiddleware() gin.HandlerFunc {
  138. return func(c *gin.Context) {
  139. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  140. c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
  141. 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")
  142. c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
  143. c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, X-session-Token")
  144. if c.Request.Method == "OPTIONS" {
  145. c.AbortWithStatus(http.StatusNoContent)
  146. return
  147. }
  148. c.Next()
  149. }
  150. }