ginUtils

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2021 License: MIT Imports: 6 Imported by: 0

README

ginTools

积累平时使用 Gin 过程中用到的的中间件等

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	SuccessRespHeader        = BaseRespHeader{Code: http.StatusOK, Msg: "ok"}
	SystemErrorRespHeader    = BaseRespHeader{Code: http.StatusInternalServerError, Msg: "系统异常"}
	ParamErrorRespHeader     = BaseRespHeader{Code: http.StatusBadRequest, Msg: "参数错误"}
	ForbiddenErrorRespHeader = BaseRespHeader{Code: http.StatusForbidden, Msg: "拒绝服务"}
	UnauthorizedRespHeader   = BaseRespHeader{Code: http.StatusUnauthorized, Msg: "未登录"}
)

Functions

func EasyHandler

func EasyHandler(cf CheckFunc, lf LogicFunc, req interface{}) gin.HandlerFunc

解析请求,整合检查请求参数,响应逻辑,并响应

Example
engine := gin.Default()
defer engine.Run(":8888")
URLPatterns(engine, "/ping", func(g *gin.RouterGroup) {
	g.POST("/", EasyHandler(easyCheck, easyServer, TestResp{}))
})

func Handler

func Handler(cf BusinessLogic, lf BusinessLogic, req interface{}) gin.HandlerFunc

Handler 类似于 EasyHandler, 但 cf 和 lf 都可以用于 net/rpc

Example
engine := gin.Default()
defer engine.Run(":8888")
URLPatterns(engine, "/ping", func(g *gin.RouterGroup) {
	g.POST("/", Handler(server, check, TestReq{}))
})

func URLPatterns

func URLPatterns(route RouteAdapter, path string, childRoute DoChildRouteFunc, middles ...gin.HandlerFunc)

URL 调度器

Types

type BaseReqInter

type BaseReqInter interface {
	JSON(ctx *gin.Context) error
}

type BaseRespHeader

type BaseRespHeader struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

type BaseRespInter

type BaseRespInter interface{}

type BusinessLogic

type BusinessLogic func(req *Request, resp *Response) error

type CheckFunc

type CheckFunc func(ctx *gin.Context, req BaseReqInter) (BaseRespInter, error)

type DoChildRouteFunc

type DoChildRouteFunc func(g *gin.RouterGroup) // 子路由

type LogicFunc

type LogicFunc func(ctx *gin.Context, req BaseReqInter) BaseRespInter

type Request

type Request struct {
	Ctx *gin.Context
	Req BaseReqInter
}

type Response

type Response struct {
	RespCode int
	Resp     BaseRespInter
}

type RouteAdapter

type RouteAdapter interface {
	Use(middleware ...gin.HandlerFunc) gin.IRoutes
	Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup
}

用于适配 Engine 和 RouteGroup

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL