rest

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableJournal

func DisableJournal(ctx Context)

DisableJournal 标识某些请求不记录journal

Types

type Context

type Context interface {

	// ShouldBindQuery 反序列化querystring
	// tag: `form:"xxx"` (注:不要写成query)
	ShouldBindQuery(obj interface{}) error

	// ShouldBindPostForm 反序列化postform(querystring会被忽略)
	// tag: `form:"xxx"`
	ShouldBindPostForm(obj interface{}) error

	// ShouldBindForm 同时反序列化querystring和postform;
	// 当querystring和postform存在相同字段时,postform优先使用。
	// tag: `form:"xxx"`
	ShouldBindForm(obj interface{}) error

	// ShouldBindJSON 反序列化postjson
	// tag: `json:"xxx"`
	ShouldBindJSON(obj interface{}) error

	// ShouldBindURI 反序列化path参数(如路由路径为 /userinfo/:name)
	// tag: `uri:"xxx"`
	ShouldBindURI(obj interface{}) error

	// Method 请求的method
	Method() string

	// Host 请求的host
	Host() string

	// Path 请求的路径(不附带querystring)
	Path() string

	// URI unescape后的uri
	URI() string

	// ContentType 请求的ContentType
	ContentType() string

	// Header clone一份请求的header
	Header() http.Header

	// Data 自定义返回数据
	Data(code int, contentType string, data []byte)

	// Redirect 重定向
	Redirect(code int, location string)

	// RequestContext 获取请求的context(当client关闭后,会自动canceled)
	RequestContext() stdctx.Context

	// FormFile 获取第一个出现的上传文件
	FormFile(name string) (*multipart.FileHeader, error)

	// GetHeader 从request中读取header
	GetHeader(key string) string

	// WriteHeader 向response中写入header
	WriteHeader(key, value string)

	// Param 获取path参数(如路由路径为 /userinfo/:name)
	// 推荐使用ShouldBindURI
	Param(key string) string

	// GetQuery 获取querystring参数
	// 推荐使用ShouldBindQuery
	GetQuery(key string) (string, bool)

	// GetPostForm 获取postform参数
	// 推荐使用ShouldBindPostForm
	GetPostForm(key string) (string, bool)

	// RawData 返回request.body
	RawData() []byte

	// Session 返回session对象
	Session() interface{}

	// Cookie 获取cookie
	Cookie(name string) (*http.Cookie, error)

	// SetCookie 回写cookie
	SetCookie(cookie *http.Cookie) error

	// GetPayload 获取payload(可以是最终返回的payload,也可以是上一个handler处理后的payload)
	GetPayload() interface{}

	// SetPayload 设置payload(可以是最终返回的payload,也可以是本次处理、传递给下一个handler的payload)
	SetPayload(payload interface{})

	// Journal 获取内部流转信息对象
	Journal() Journal

	// Logger 获取日志实例
	Logger() *zap.Logger

	// AbortWithError 终止并处理错误
	AbortWithError(err Error)

	Alias() string
	// contains filtered or unexported methods
}

Context 上下文、支持方法包装

type ErrReply

type ErrReply struct {
	Code int    `json:"code"`
	Desc string `json:"desc"`
}

ErrReply 发生错误时返回的内容定义

type Error

type Error interface {
	error
	// WithHTTPCode 设置返回的http code
	WithHTTPCode(httpCode int) Error
	// WithErr 设置真实发生的err,推荐使用github.com/pkg/errors包装一下stack信息,便于快速定位err发生的真实位置。
	WithErr(err error) Error
	// String 返回JSON格式的错误详情
	String() string
	// contains filtered or unexported methods
}

Error 包含返回码、描述语、错误的包装类

func NewError

func NewError(businessCode int, desc string) Error

NewError 创建一个新的Error包装类

func ToError

func ToError(err error) (Error, bool)

ToError 尝试转换err

type HandlerFunc

type HandlerFunc func(Context)

HandlerFunc 逻辑处理handler定义

func AliasForRecordMetrics

func AliasForRecordMetrics(path string) HandlerFunc

AliasForRecordMetrics 对请求uri起个别名,用于prometheus记录指标。 如:Get /userinfo/:username 这样的uri,因为username会有非常多的情况,这样记录prometheus指标会非常的不有好。

func WrapSessionHandler

func WrapSessionHandler(handler func(Context) (session interface{}, err error)) HandlerFunc

WrapSessionHandler 用来处理session或token的入口,在之后的handler中只需ctx.Session()即可。 如果handler内部出现错误,推荐返回rest.Error类型的错误以便自定义提示语,否则将默认返回"Internal Server Error"。

type IRoutes

type IRoutes interface {
	Any(string, ...HandlerFunc)
	GET(string, ...HandlerFunc)
	POST(string, ...HandlerFunc)
	DELETE(string, ...HandlerFunc)
	PATCH(string, ...HandlerFunc)
	PUT(string, ...HandlerFunc)
	OPTIONS(string, ...HandlerFunc)
	HEAD(string, ...HandlerFunc)
	Static(relativePath, root string)
	StaticFS(relativePath string, fs http.FileSystem)
}

IRoutes 包装gin的IRoutes

type Journal

type Journal = journal.T

Journal 记录内部流转信息

type Mux

type Mux interface {
	http.Handler
	Group(relativePath string, handlers ...HandlerFunc) RouterGroup
}

Mux http mux

func NewMux

func NewMux(logger *zap.Logger, options ...Option) (Mux, error)

NewMux 创建http mux实例

type OnPanicNotify

type OnPanicNotify func(ctx Context, err interface{}, stackInfo string)

OnPanicNotify 发生panic时通知用

type Option

type Option func(*option)

Option 自定义选项

func WithDisablePProf

func WithDisablePProf() Option

WithDisablePProf 禁用pprof

func WithDisableproPrometheus

func WithDisableproPrometheus() Option

WithDisableproPrometheus 禁用prometheus

func WithEnableCors

func WithEnableCors() Option

WithEnableCors 开启CORS

func WithMarshalJournal

func WithMarshalJournal() Option

WithMarshalJournal marshal journal to json string

func WithPanicNotify

func WithPanicNotify(notify OnPanicNotify) Option

WithPanicNotify 设置panic时的通知回调

func WithRecordMetrics

func WithRecordMetrics(recoder RecordMetrics) Option

WithRecordMetrics 设置记录prometheus记录指标回调

type RecordMetrics

type RecordMetrics func(method, uri string, success bool, httpCode, businessCode int, costSeconds float64)

RecordMetrics 记录prometheus指标用 如果使用AliasForRecordMetrics配置了别名,uri将被替换为别名。

type RouterGroup

type RouterGroup interface {
	Group(string, ...HandlerFunc) RouterGroup
	IRoutes
}

RouterGroup 包装gin的RouterGroup

Jump to

Keyboard shortcuts

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