response

package
v0.0.0-...-796898b Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package response 请求返回处理包

Index

Constants

View Source
const (
	// Success 请求返回成功码
	Success = "200"
	// Fail 请求返回失败码
	Fail = "9999"
)

Variables

View Source
var (
	// ExpNotExist 所请求事务不存在异常
	ExpNotExist = Exp("not exist", http.StatusOK)
)

Functions

This section is empty.

Types

type Exception

type Exception struct {
	Msg string // 异常通用信息
	// contains filtered or unexported fields
}

Exception 自定义异常实体

func Exp

func Exp(brief string, httpCode int) Exception

Exp 返回新自定义异常

func (*Exception) Fit

func (exception *Exception) Fit(prefix string) *Exception

Fit 为内置异常补充异常信息内容前缀

type RespError

type RespError struct {
	ErrorCode string
	ErrorMsg  string

	// HTTPStatusCode http的错误编码
	HTTPStatusCode int
}

RespError 自定义 error 对象

func (*RespError) Error

func (resErr *RespError) Error() string

Error 实现error接口,自定义error

func (*RespError) FormatMsg

func (resErr *RespError) FormatMsg(args ...interface{}) *RespError

FormatMsg 添加参数

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response 提供实例化调用 Do 方法,并内置返回策略

func SyncPoolGetResponse

func SyncPoolGetResponse() *Response

SyncPoolGetResponse 提供实例化调用 Do 方法,并内置返回策略

func (*Response) Do

func (response *Response) Do(context *gin.Context, objBlock func(result *Result))

Do 处理 request 请求

context:请求上下文

obj:请求中 body 中内容期望转换的对象并做空实例化,如 new(Type)

objBlock:obj 对象的回调方法,最终调用 Do 函数的方法会接收到返回值

objBlock interface{}:obj 对象的回调方法所返回的最终交由 response 输出的对象

objBlock error:obj 对象的回调方法所返回的错误对象

如未出现err,且无可描述返回内容,则返回值可为 (nil, nil)

func (*Response) DoSelf

func (response *Response) DoSelf(context *gin.Context, self func(writer http.ResponseWriter, request *http.Request))

DoSelf 处理 request 请求

context:请求上下文

obj:请求中 body 中内容期望转换的对象并做空实例化,如 new(Type)

objBlock:obj 对象的回调方法,最终调用 Do 函数的方法会接收到返回值

objBlock interface{}:obj 对象的回调方法所返回的最终交由 response 输出的对象

objBlock error:obj 对象的回调方法所返回的错误对象

如未出现err,且无可描述返回内容,则返回值可为 (nil, nil)

type Result

type Result struct {
	ResultCode string `json:"code"`
	Msg        string `json:"msg"`
	// 数据接口
	Data interface{} `json:"data"`
}

Result 请求返回对象实体

func (*Result) Callback

func (result *Result) Callback(callback func() *Result, err error)

Callback 返回结果对象介入降级操作方法

func (*Result) Fail

func (result *Result) Fail(msg string)

Fail 方法主要提供返回错误的json数据

func (*Result) FailErr

func (result *Result) FailErr(err error)

FailErr 携带error信息,如果是respError,则 必然存在errorCode和msg,因此进行赋值。否则不赋值

func (*Result) Say

func (result *Result) Say(context *gin.Context)

Say response 返回自身

func (*Result) SayFail

func (result *Result) SayFail(context *gin.Context, msg string)

SayFail response 返回请求失败对象

func (*Result) SaySuccess

func (result *Result) SaySuccess(context *gin.Context, obj interface{})

SaySuccess response 返回请求成功对象

func (*Result) Success

func (result *Result) Success(obj interface{})

Success 默认成功返回

func (*Result) Write

func (result *Result) Write(context *gin.Context, byte []byte) (int, error)

Write response 字节

type Router

type Router struct {
	Engine  *gin.Engine
	Context *gin.Context
	Group   *gin.RouterGroup
}

Router 路由入口对象

func (*Router) Any

func (router *Router) Any(relativePath string, f func(router *Router))

Any 路由 Any 请求方法

func (*Router) DELETE

func (router *Router) DELETE(relativePath string, f func(router *Router))

DELETE 路由 DELETE 请求方法

func (*Router) GET

func (router *Router) GET(relativePath string, f func(router *Router))

GET 路由 GET 请求方法

func (*Router) HEAD

func (router *Router) HEAD(relativePath string, f func(router *Router))

HEAD 路由 HEAD 请求方法

func (*Router) Handle

func (router *Router) Handle(relativePath string, f func(router *Router))

Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in GitHub.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Router) OPTIONS

func (router *Router) OPTIONS(relativePath string, f func(router *Router))

OPTIONS 路由 OPTIONS 请求方法

func (*Router) PATCH

func (router *Router) PATCH(relativePath string, f func(router *Router))

PATCH 路由 PATCH 请求方法

func (*Router) POST

func (router *Router) POST(relativePath string, f func(router *Router))

POST 路由 POST 请求方法

func (*Router) PUT

func (router *Router) PUT(relativePath string, f func(router *Router))

PUT 路由 PUT 请求方法

func (*Router) Static

func (router *Router) Static(relativePath, root string)

Static 路由 Static 请求方法 Static serves files from the given file system root. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use :

router.Static("/static", "/var/www")

func (*Router) StaticFS

func (router *Router) StaticFS(relativePath string, fs http.FileSystem)

StaticFS 路由 StaticFS 请求方法 StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead. Gin by default user: gin.Dir()

func (*Router) StaticFile

func (router *Router) StaticFile(relativePath, filepath string)

StaticFile 路由 StaticFile 请求方法 StaticFile registers a single route in order to serve a single file of the local filesystem. router.StaticFile("favicon.ico", "./resources/favicon.ico")

func (*Router) Use

func (router *Router) Use(relativePath string, f func(router *Router))

Use 路由 Use 请求方法

Jump to

Keyboard shortcuts

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