middleware

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RequestParam = "request_param"

* gin context全局变量用于存储传参,返回,错误日志

View Source
var ResponseBody = "response_body"
View Source
var ResponseErr = "response_err"

Functions

func Error

func Error(c *gin.Context, errMsg error, errCode ...int)

通用错误

func InitHttp

func InitHttp(prefix string) *gin.Engine

对外提供初始化路由的方法

func ParamError

func ParamError(c *gin.Context)

参数错误

func RegisterInterceptor

func RegisterInterceptor(f func(g *gin.Context))

用于controller注册拦截器

func RegisterParam

func RegisterParam(paramName string, f func(g *gin.Context) (string, error))

func RegisterRouter

func RegisterRouter(f func(g *gin.RouterGroup))

用于controller注册路由

func Success

func Success(c *gin.Context, data ...any)

* 封装返回

通用成功返回

func Valid

func Valid(data interface{}) error

通用校验器

Types

type AbstractHandler

type AbstractHandler struct {
}

func (*AbstractHandler) ContextExec

func (s *AbstractHandler) ContextExec(c *gin.Context) (any, error)

func (*AbstractHandler) ContextHandler

func (s *AbstractHandler) ContextHandler(c *gin.Context, service Handler) (any, error)

func (*AbstractHandler) Exec

func (s *AbstractHandler) Exec() (any, error)

真正业务类需要实现的方法

func (*AbstractHandler) Handler

func (s *AbstractHandler) Handler(service Handler) (any, error)

controller调用业务入口,使用多态的特性执行-若需要增强可直接重载该方法

type AbstractParser

type AbstractParser[T Handler] struct {
	C         *gin.Context
	IsContext bool
	// contains filtered or unexported fields
}

func (*AbstractParser[T]) Body

func (b *AbstractParser[T]) Body(params ...string)

通用并从body中获取参数传递

func (*AbstractParser[T]) Form

func (b *AbstractParser[T]) Form(params ...string)

通用从 form 中获取文件或参数并反射到serviceParam中并执行

func (*AbstractParser[T]) Path

func (b *AbstractParser[T]) Path(params ...string)

通用从 param 获取参数传递:url/{id}/{params...}

func (*AbstractParser[T]) Query

func (b *AbstractParser[T]) Query(params ...string)

通用从 Query 获取参数传递:url?id=1

type Handler

type Handler interface {
	//常规业务函数,返回data
	Handler(service Handler) (any, error)
	//将context传递到全局
	ContextHandler(c *gin.Context, service Handler) (any, error)
	//需要重载的接口
	Exec() (any, error)
	ContextExec(c *gin.Context) (any, error)
}

对外提供业务服务的handler接口(多态特性)

type NodeResult

type NodeResult[T any] struct {
	Node      T               `json:"node"`
	ChildList []NodeResult[T] `json:"childList"`
}

type PageResponse

type PageResponse[T any] struct {
	Response[T]
	//当前页
	CurrentPage int `json:"currentPage"`
	//每页数量
	PageSize int `json:"pageSize"`
	//总页数
	PageCount int `json:"pageCount"`
	//总数量
	TotalCount int `json:"totalCount"`
}

func PageResult

func PageResult(count int64, list any, pageSize int, currentPage int) PageResponse[any]

封装分页返回数据

type Parser

type Parser interface {
	//解析方式
	Body(params ...string)
	Query(params ...string)
	Path(params ...string)
	Form(params ...string)
}

* 参数解析器 1、分组各类传参 2、传参校验 3、根据传参转发到指定handler 4、封装返回

type Response

type Response[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data T      `json:"data"`
}

* 标准类型的返回封装

Jump to

Keyboard shortcuts

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