router.go 4.9 KB

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