gopkg/gin/util/responsed.go

97 lines
2.7 KiB
Go
Raw Normal View History

2021-07-24 20:32:57 +08:00
// Package util ...
2021-03-09 17:52:14 +08:00
//
// Description : 结合gin框架的一些工具集
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-03-09 4:51 下午
package util
import (
2021-03-09 20:37:08 +08:00
"fmt"
2021-03-09 17:52:14 +08:00
"net/http"
2021-03-09 20:37:08 +08:00
"strings"
2021-03-09 17:52:14 +08:00
"time"
"github.com/gin-gonic/gin"
)
// Response 向客户端响应数据
//
// Author : go_developer@163.com<张德满>
//
// Date : 4:52 下午 2021/3/9
func Response(ctx *gin.Context, code interface{}, message string, data interface{}) {
var responseData = gin.H{
"code": code,
"message": message,
"data": data,
"trace_id": ctx.GetString("trace_id"),
"cost": time.Since(time.Unix(ctx.GetInt64("start_time"), 0)).Seconds(),
}
ctx.JSON(http.StatusOK, responseData)
}
2021-03-09 20:37:08 +08:00
// RegisterRouter 注册gin路由
//
// Author : go_developer@163.com<张德满>
//
// Date : 8:36 下午 2021/3/9
2021-03-26 14:34:24 +08:00
func RegisterRouter(router *gin.Engine, method string, uri string, handler gin.HandlerFunc, middlewareList []gin.HandlerFunc) error {
if nil == middlewareList {
middlewareList = make([]gin.HandlerFunc, 0)
}
2021-03-09 20:37:08 +08:00
switch strings.ToUpper(method) {
case http.MethodGet:
2021-03-26 14:34:24 +08:00
router.GET(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case http.MethodPost:
2021-03-26 14:34:24 +08:00
router.POST(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case http.MethodDelete:
2021-03-26 14:34:24 +08:00
router.DELETE(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case http.MethodHead:
2021-03-26 14:34:24 +08:00
router.HEAD(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case http.MethodOptions:
2021-03-26 14:34:24 +08:00
router.OPTIONS(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case http.MethodPatch:
2021-03-26 14:34:24 +08:00
router.PATCH(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case http.MethodPut:
2021-03-26 14:34:24 +08:00
router.PUT(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
case "ANY": // 一次性注册全部请求方法的路由
2021-03-26 14:34:24 +08:00
router.Any(uri, handler).Use(middlewareList...)
2021-03-09 20:37:08 +08:00
default:
// 不是一个函数,数名method配置错误
return fmt.Errorf("uri=%s method=%s 请求方法配置错误", uri, method)
}
return nil
}
2021-03-09 20:40:55 +08:00
// RegisterRouterGroup 注册gin路由
//
// Author : go_developer@163.com<张德满>
//
// Date : 8:36 下午 2021/3/9
func RegisterRouterGroup(router *gin.RouterGroup, method string, uri string, handler gin.HandlerFunc) error {
switch strings.ToUpper(method) {
case http.MethodGet:
router.GET(uri, handler)
case http.MethodPost:
router.POST(uri, handler)
case http.MethodDelete:
router.DELETE(uri, handler)
case http.MethodHead:
router.HEAD(uri, handler)
case http.MethodOptions:
router.OPTIONS(uri, handler)
case http.MethodPatch:
router.PATCH(uri, handler)
case http.MethodPut:
router.PUT(uri, handler)
case "ANY": // 一次性注册全部请求方法的路由
router.Any(uri, handler)
default:
// 不是一个函数,数名method配置错误
return fmt.Errorf("uri=%s method=%s 请求方法配置错误", uri, method)
}
return nil
}