作者 yangfu

增加:gin接入

@@ -5,6 +5,7 @@ go 1.14 @@ -5,6 +5,7 @@ go 1.14
5 require ( 5 require (
6 github.com/astaxie/beego v1.12.1 6 github.com/astaxie/beego v1.12.1
7 github.com/dgrijalva/jwt-go v3.2.0+incompatible 7 github.com/dgrijalva/jwt-go v3.2.0+incompatible
  8 + github.com/gin-gonic/gin v1.4.0
8 github.com/go-pg/pg/v10 v10.0.0-beta.2 9 github.com/go-pg/pg/v10 v10.0.0-beta.2
9 github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9 10 github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9
10 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect 11 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg" 7 _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg"
8 _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/redis" 8 _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/redis"
9 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log" 9 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
  10 + //"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/ginsvr"
10 _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr" 11 _ "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/appsvr"
11 ) 12 )
12 13
@@ -16,5 +17,6 @@ func main() { @@ -16,5 +17,6 @@ func main() {
16 }() 17 }()
17 log.Info("app start!") 18 log.Info("app start!")
18 log.Info(constant.POSTGRESQL_DB_NAME) 19 log.Info(constant.POSTGRESQL_DB_NAME)
  20 + //ginsvr.Run()
19 beego.Run() 21 beego.Run()
20 } 22 }
@@ -2,11 +2,11 @@ package constant @@ -2,11 +2,11 @@ package constant
2 2
3 import "os" 3 import "os"
4 4
5 -var POSTGRESQL_DB_NAME = "partner_dev" //partner  
6 -var POSTGRESQL_USER = "postgres" //  
7 -var POSTGRESQL_PASSWORD = "postgres_15432" //pgsql@123  
8 -var POSTGRESQL_HOST = "101.37.68.23" //127.0.0.1  
9 -var POSTGRESQL_PORT = "15432" //5432 5 +var POSTGRESQL_DB_NAME = "partner_dev" //partner
  6 +var POSTGRESQL_USER = "postgres" //
  7 +var POSTGRESQL_PASSWORD = "eagle1010" //pgsql@123
  8 +var POSTGRESQL_HOST = "114.55.200.59" //127.0.0.1
  9 +var POSTGRESQL_PORT = "31543" //5432
10 var DISABLE_CREATE_TABLE = false 10 var DISABLE_CREATE_TABLE = false
11 var DISABLE_SQL_GENERATE_PRINT = false 11 var DISABLE_SQL_GENERATE_PRINT = false
12 12
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/astaxie/beego/validation"
  5 + "github.com/gin-gonic/gin"
  6 + "github.com/tiptok/gocomm/pkg/mygin"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
  8 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
  9 + "strconv"
  10 +)
  11 +
  12 +type BaseController struct {
  13 + mygin.BaseController
  14 +}
  15 +
  16 +func (this *BaseController) Resp(c *gin.Context, httpCode int, rsp *protocol.ResponseMessage) {
  17 + c.Set("outputData", rsp)
  18 + c.JSON(httpCode, rsp)
  19 + //c.Abort()
  20 +}
  21 +
  22 +func (controller *BaseController) Valid(obj interface{}) (result bool, msg *protocol.ResponseMessage) {
  23 + /*校验*/
  24 + var err error
  25 + valid := validation.Validation{}
  26 + result, err = valid.Valid(obj)
  27 + if err != nil {
  28 + }
  29 + if !result {
  30 + msg = protocol.BadRequestParam(2)
  31 + return
  32 + }
  33 +
  34 + return
  35 +}
  36 +
  37 +//获取请求头信息
  38 +func (this *BaseController) GetRequestHeader(c *gin.Context) *protocol.RequestHeader {
  39 + h := &protocol.RequestHeader{}
  40 +
  41 + h.AccessToken = c.Query("x-mmm-accesstoken")
  42 + h.AppProject = c.Query("x-mmm-appproject")
  43 + h.DeviceType, _ = strconv.Atoi(c.Query("x-mmm-devicetype"))
  44 + h.Sign = c.Query("x-mmm-sign")
  45 + h.Uuid = c.Query("x-mmm-uuid")
  46 + h.TimeStamp = c.Query("x-mmm-timestamp")
  47 + h.Version = c.Query("x-mmm-version")
  48 + h.UserId, _ = strconv.ParseInt(c.Query("x-mmm-id"), 10, 64)
  49 +
  50 + if len(h.AccessToken) > 0 {
  51 + if claim, err := utils.ParseJWTToken(h.AccessToken); err == nil {
  52 + h.UserId = claim.UserId
  53 + }
  54 + }
  55 + return h
  56 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/gin-gonic/gin"
  5 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/order"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
  8 + "net/http"
  9 + "time"
  10 +)
  11 +
  12 +type OrderController struct {
  13 + BaseController
  14 +}
  15 +
  16 +//OrderStatistics 订单统计
  17 +// @router /statistics [post]
  18 +func (this *OrderController) Statistics(c *gin.Context) {
  19 + var msg *protocol.ResponseMessage
  20 + defer func() {
  21 + this.Resp(c, http.StatusOK, msg)
  22 + }()
  23 + var request *protocol.OrderStatisticsRequest
  24 + if err := c.ShouldBind(&request); err != nil {
  25 + msg = protocol.BadRequestParam(1)
  26 + return
  27 + }
  28 + if b, m := this.Valid(request); !b {
  29 + msg = m
  30 + return
  31 + }
  32 + header := this.GetRequestHeader(c)
  33 + msg = protocol.NewReturnResponse(order.Statistics(header, request))
  34 +}
  35 +
  36 +//OrderDetail 订单详情
  37 +// @router /orderDetail [post]
  38 +func (this *OrderController) OrderDetail(c *gin.Context) {
  39 + var msg *protocol.ResponseMessage
  40 + defer func() {
  41 + this.Resp(c, http.StatusOK, msg)
  42 + }()
  43 + var request *protocol.OrderDetailRequest
  44 + if err := c.ShouldBind(&request); err != nil {
  45 + msg = protocol.BadRequestParam(1)
  46 + return
  47 + }
  48 + if b, m := this.Valid(request); !b {
  49 + msg = m
  50 + return
  51 + }
  52 + header := this.GetRequestHeader(c)
  53 + msg = protocol.NewReturnResponse(order.Detail(header, request))
  54 +}
  55 +
  56 +//OrderList
  57 +func (this *OrderController) OrderList(c *gin.Context) {
  58 + var msg *protocol.ResponseMessage
  59 + defer func() {
  60 + this.Resp(c, http.StatusOK, msg)
  61 + }()
  62 + var request *protocol.OrderListRequest
  63 + if err := c.ShouldBind(&request); err != nil {
  64 + msg = protocol.BadRequestParam(1)
  65 + return
  66 + }
  67 + if b, m := this.Valid(request); !b {
  68 + msg = m
  69 + return
  70 + }
  71 + request.OrderType = domain.OrderReal
  72 + header := this.GetRequestHeader(c)
  73 + msg = protocol.NewReturnResponse(order.List(header, request))
  74 +}
  75 +
  76 +//OrderList
  77 +func (this *OrderController) Intentions(c *gin.Context) {
  78 + var msg *protocol.ResponseMessage
  79 + defer func() {
  80 + this.Resp(c, http.StatusOK, msg)
  81 + }()
  82 + var request *protocol.OrderListRequest
  83 + if err := c.ShouldBind(&request); err != nil {
  84 + msg = protocol.BadRequestParam(1)
  85 + return
  86 + }
  87 + if b, m := this.Valid(request); !b {
  88 + msg = m
  89 + return
  90 + }
  91 + request.EndTime = time.Now().Unix() * 1000
  92 + request.OrderType = domain.OrderIntention
  93 + header := this.GetRequestHeader(c)
  94 + msg = protocol.NewReturnResponse(order.List(header, request))
  95 +}
  1 +package middleware
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "fmt"
  6 + "github.com/gin-gonic/gin"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
  8 + "io/ioutil"
  9 + "net/http"
  10 + "time"
  11 +)
  12 +
  13 +func CreateRequstLogFilter() gin.HandlerFunc {
  14 + return func(ctx *gin.Context) {
  15 + requestId := fmt.Sprintf("%v.%v", ctx.Request.Method, ctx.Request.RequestURI)
  16 + ctx.Set("requestId", requestId)
  17 + var body string = "{}"
  18 + if _, ok := ctx.Get("requestBody"); ok {
  19 + data, _ := ctx.Get("requestBody")
  20 + body = string(data.([]byte))
  21 + }
  22 + start := time.Now()
  23 + ctx.Next()
  24 + end := time.Now()
  25 + latency := end.Sub(start)
  26 +
  27 + v, _ := ctx.Get("outputData")
  28 + rspBody, _ := json.Marshal(v)
  29 + if len(rspBody) > 500 {
  30 + rspBody = rspBody[:500]
  31 + }
  32 + log.Debug(fmt.Sprintf("====>Recv User:%v | %v | %s | %v | \nAuth=%v \nReqBody:%s \nRspBody:%v", ctx.Query("UserId"), ctx.ClientIP(), requestId, latency, ctx.Query("x-mmm-accesstoken"), body, string(rspBody)))
  33 + }
  34 +}
  35 +
  36 +func CreateResponseLogFilter() func(ctx *gin.Context) {
  37 + return func(ctx *gin.Context) {
  38 + requestId, _ := ctx.Get("requestId")
  39 + v, _ := ctx.Get("outputData")
  40 + body, _ := json.Marshal(v)
  41 + if len(body) > 1000 {
  42 + body = body[:1000]
  43 + }
  44 + log.Debug(fmt.Sprintf("<====Send RequestId:%v BodyData:%s", requestId, body))
  45 + }
  46 +}
  47 +
  48 +func CreateRequestBodyFilter() func(ctx *gin.Context) {
  49 + return func(ctx *gin.Context) {
  50 + if ctx.Request.Method == http.MethodPost || ctx.Request.Method == http.MethodPut {
  51 + body, _ := ioutil.ReadAll(ctx.Request.Body)
  52 + if len(body) == 0 {
  53 + body = []byte("{}")
  54 + }
  55 + ctx.Set("requestBody", body)
  56 + ctx.Request.Body.Close()
  57 + }
  58 + }
  59 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/gin-gonic/gin/ginS"
  5 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/ginsvr/controllers"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/port/ginsvr/middleware"
  7 +)
  8 +
  9 +func init() {
  10 + //ginS.Use(middleware.CreateRequestBodyFilter())
  11 + ginS.Use(middleware.CreateRequstLogFilter())
  12 +
  13 + order := &controllers.OrderController{}
  14 + ginS.POST("/order/statistics", order.Statistics)
  15 + ginS.POST("/order/details", order.OrderDetail)
  16 + ginS.POST("/order/list", order.OrderList)
  17 + ginS.POST("/order/intentions", order.Intentions)
  18 +}