httpkit

package module
v0.0.0-...-845e1c7 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Render http response content
	Render = renderer.New()
)
View Source
var RequestDecoder = schema.NewDecoder()

RequestDecoder decode request values

View Source
var (
	// RequestLogger 请求日志记录,可替换
	RequestLogger = func(w http.ResponseWriter, r *http.Request, logger logrus.FieldLogger) logrus.FieldLogger {
		rl := r.URL

		fields := logrus.Fields{
			"remote": r.RemoteAddr,
			"method": r.Method,
			"path":   rl.Path,
		}

		if v := rl.RawQuery; v != "" {
			if q := rl.Query(); len(q) > 0 {
				fields["query"] = q
			}
		}

		if m := r.Method; m == http.MethodPost || m == http.MethodPut || m == http.MethodPatch {
			r.ParseForm()
			if form := r.PostForm; len(form) > 0 {
				fields["form"] = form
			}
		}

		return logger.WithFields(fields)
	}
)

Functions

func GetCaller

func GetCaller() *runtime.Frame

GetCaller 返回当前函数的调用者

func LogRequest

func LogRequest(logger logrus.FieldLogger) func(http.Handler) http.Handler

LogRequest http访问日志

func MustScanJSON

func MustScanJSON(dst interface{}, r io.Reader)

MustScanJSON json decode request body, panic when error

func MustScanValue

func MustScanValue(dst interface{}, values url.Values)

MustScanValue deocde request values, panic when error

func Recoverer

func Recoverer(logger logrus.FieldLogger) func(http.Handler) http.Handler

Recoverer recover panic

func ScanJSON

func ScanJSON(dst interface{}, r io.Reader) error

ScanJSON decode json and validate

func ScanValues

func ScanValues(dst interface{}, values url.Values) error

ScanValues 从url.Values解析数据

func WriteError

func WriteError(w http.ResponseWriter, httpError *Error) error

WriteError write error response

Types

type Error

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

Error http错误

func NewError

func NewError(code int) *Error

NewError 创建http错误

func WrapError

func WrapError(err error) *Error

WrapError 把其它错误转换为http错误

func WrapErrorf

func WrapErrorf(format string, err error) *Error

WrapErrorf 以指定的格式把错误转换为http错误

func (Error) Body

func (e Error) Body() io.Reader

Body returns response body reader

func (Error) Caller

func (e Error) Caller() (*runtime.Frame, bool)

Caller returns WrapError caller

func (Error) Error

func (e Error) Error() string

func (Error) Header

func (e Error) Header() http.Header

Header returns response header

func (*Error) Panic

func (e *Error) Panic()

Panic with error

func (Error) StatusCode

func (e Error) StatusCode() int

StatusCode returns response status code

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap returns cause error

func (*Error) WithBody

func (e *Error) WithBody(r io.Reader) *Error

WithBody set response body

func (*Error) WithBytes

func (e *Error) WithBytes(data []byte) *Error

WithBytes set response body content

func (*Error) WithHeader

func (e *Error) WithHeader(key, value string) *Error

WithHeader set response header

func (*Error) WithJSON

func (e *Error) WithJSON(v interface{}) error

WithJSON set json content as response body

func (*Error) WithStatus

func (e *Error) WithStatus(code int) *Error

WithStatus set response status code

func (*Error) WithString

func (e *Error) WithString(s string) *Error

WithString set response body content

type M

type M map[string]interface{}

M describes handy type that represents data to send as response

type Pagination

type Pagination struct {
	First    int `json:"first"`
	Last     int `json:"last"`
	Previous int `json:"previous"`
	Current  int `json:"current"`
	Next     int `json:"next"`
	Size     int `json:"size"`
	Items    int `json:"items"`
}

Pagination 数据库分页计算

func NewPagination

func NewPagination(current, size, items int) Pagination

NewPagination 计算分页页码

func (Pagination) Limit

func (p Pagination) Limit() int

Limit 数据库查询LIMIT值

func (Pagination) Offset

func (p Pagination) Offset() int

Offset 数据库查询OFFSET值

Jump to

Keyboard shortcuts

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