http/middleware.go
2022-10-31 17:22:49 +08:00

33 lines
613 B
Go

/**
* @Author: Echo
* @Email: 1711788888@qq.com
* @Date: 2021/8/16 9:47 上午
* @Desc: request's middleware
*/
package http
type MiddlewareFunc = func(r *Request) (*Response, error)
const middlewareKey = "__httpClientMiddlewareKey"
type middleware struct {
err error
req *Request
resp *Response
index int
handlers []MiddlewareFunc
}
// Next exec the next middleware.
func (m *middleware) Next() (*Response, error) {
if m.index < len(m.handlers) {
m.index++
if m.resp, m.err = m.handlers[m.index](m.req); m.err != nil {
return m.resp, m.err
}
}
return m.resp, m.err
}