router.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package routers
  2. import (
  3. "mtp2_if/controllers/cptrade"
  4. "mtp2_if/controllers/wrtrade"
  5. "mtp2_if/logger"
  6. "mtp2_if/token"
  7. "net/http"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. // Swagger生成的文档
  11. _ "mtp2_if/docs"
  12. ginSwagger "github.com/swaggo/gin-swagger"
  13. "github.com/swaggo/gin-swagger/swaggerFiles"
  14. )
  15. // InitRouter 初始化路由器的方法
  16. func InitRouter() *gin.Engine {
  17. r := gin.New()
  18. // 设置日志中间件
  19. r.Use(ginLoggerMiddleware())
  20. // 设置奔溃中间件
  21. r.Use(ginRecoveryMiddleware())
  22. // 跨域
  23. r.Use(CORSMiddleware())
  24. // Swagger
  25. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  26. // 路由分组
  27. apiR := r.Group("/api")
  28. apiR.Use(token.Auth())
  29. // ************************ 仓单贸易 ************************
  30. wrTradeR := apiR.Group("WRTrade")
  31. wrTradeR.Use()
  32. {
  33. wrTradeR.GET("/GetAllDeliveryGoods", wrtrade.GetAllDeliveryGoods)
  34. }
  35. // ************************ 产能预售 ************************
  36. cpTradeR := apiR.Group("CPTrade")
  37. cpTradeR.Use()
  38. {
  39. // 查询产能预售申请表
  40. cpTradeR.GET("/QueryPreasleApply", cptrade.QueryPreasleApply)
  41. // 查询远期订单信息
  42. cpTradeR.GET("/QueryUserGoodsData", cptrade.QueryUserGoodsData)
  43. // 查询远期订单注销申请信息
  44. cpTradeR.GET("/QueryPositionCancel", cptrade.QueryPositionCancel)
  45. // 查询产能预售商品扩展信息
  46. cpTradeR.GET("/QueryPresaleGoodsEx", cptrade.QueryPresaleGoodsEx)
  47. }
  48. return r
  49. }
  50. func ginLoggerMiddleware() gin.HandlerFunc {
  51. return func(c *gin.Context) {
  52. start := time.Now()
  53. c.Next()
  54. end := time.Now()
  55. latency := end.Sub(start)
  56. path := c.Request.URL.RequestURI()
  57. clientip := c.ClientIP()
  58. method := c.Request.Method
  59. statuscode := c.Writer.Status()
  60. logger.GetLogger().Infof("|%3d|%13v|%15s|%s %s",
  61. statuscode,
  62. latency,
  63. clientip,
  64. method,
  65. path)
  66. }
  67. }
  68. func ginRecoveryMiddleware() gin.HandlerFunc {
  69. return gin.RecoveryWithWriter(logger.GetLogWriter())
  70. }
  71. // CORSMiddleware cors跨域.
  72. func CORSMiddleware() gin.HandlerFunc {
  73. return func(c *gin.Context) {
  74. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  75. c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
  76. 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")
  77. c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
  78. c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type, X-session-Token")
  79. if c.Request.Method == "OPTIONS" {
  80. c.AbortWithStatus(http.StatusNoContent)
  81. return
  82. }
  83. c.Next()
  84. }
  85. }