Documentation
¶
Overview ¶
Package http 提供基本的http服务
Index ¶
- Constants
- func CheckRedirectError(err error) (url string, ok bool)
- func CheckRequestError(err error) (status int, body []byte, ok bool)
- func ErrorFromRequestContext(req *http.Request) (bool, error)
- func FromRequestContext(req *http.Request, key interface{}) interface{}
- func GetFloat32Parameter(r url.Values, name string) (val float32, err error)
- func GetFloat64Parameter(r url.Values, name string) (val float64, err error)
- func GetInt32Parameter(r url.Values, name string) (val int32, err error)
- func GetInt64Parameter(r url.Values, name string) (val int64, err error)
- func GetParameter(r url.Values, name string) string
- func GetURL(client *http.Client, url string, params url.Values) (string, error)
- func GetURLRaw(client *http.Client, url string, params url.Values, reqHeader http.Header, ...) (header http.Header, body []byte, err error)
- func GetURLRawToWriter(client *http.Client, url string, params url.Values, reqHeader http.Header, ...) (header http.Header, err error)
- func GetURLRawToWriterWithContext(ctx context.Context, client *http.Client, url string, params url.Values, ...) (header http.Header, err error)
- func GetURLWithCookie(client *http.Client, url string, params url.Values, cookies map[string]string) (string, error)
- func NewRedirectError(req *http.Request, via []*http.Request) error
- func ParseParams(r url.Values, dest interface{}) error
- func Post(client *http.Client, url string, params url.Values, header map[string]string, ...) ([]byte, http.Header, error)
- func PostURL(client *http.Client, url string, params url.Values, contentType string, ...) ([]byte, http.Header, error)
- func PostURLWithCookie(client *http.Client, url string, params url.Values, contentType string, ...) ([]byte, http.Header, error)
- func PostURLWithCookieAndHeader(client *http.Client, url string, params url.Values, header map[string]string, ...) ([]byte, http.Header, error)
- func RegJSONMarshaler(marshaler JSONMarshaler)
- func RenderHTML(w http.ResponseWriter, data []byte)
- func RenderJSON(w http.ResponseWriter, jsonData interface{})
- func RenderTemplate(w http.ResponseWriter, templateDir, tmpl string, data interface{})
- func RenderText(w http.ResponseWriter, text string)
- func RequestWithContext(req *http.Request, key, val interface{}) *http.Request
- func RequestWithError(req *http.Request, err error) *http.Request
- func ToUnderlineName(camelName string) string
- type BaseController
- type Config
- func (p *Config) InitWithInjector(injector *inject.Injector) error
- func (p *Config) RegController(controller Controller) error
- func (p *Config) RegHandleFunc(patternPath string, handlerFunc http.HandlerFunc) error
- func (p *Config) RegMiddleware(middleware Middleware) error
- func (p *Config) RegStaticFunc(patternAndPath map[string]string) error
- type Controller
- type GraceableHandler
- type JSONMarshaler
- type Middleware
- type MiddlewareFunc
- type NoDirFS
- type PostPrepare
- type RedirectError
- type RequestError
- type RequestMehotdMiddleware
- type Resp
- type ResponseHandler
- type RetrivedCookieJar
- type Service
Constants ¶
const ( CacheControl = "Cache-Control" XCacheControl = "X-" + CacheControl )
http header
Variables ¶
This section is empty.
Functions ¶
func CheckRedirectError ¶
CheckRedirectError if err is RedirectError ,then return redirect error
func CheckRequestError ¶
CheckRequestError if err is RequestError,then return response status code and body
func ErrorFromRequestContext ¶
ErrorFromRequestContext 从req的context取得错误值
func FromRequestContext ¶
FromRequestContext 从req的context中取得key值
func GetFloat32Parameter ¶
GetFloat32Parameter 取得由name指定的32位浮点数参数值
func GetFloat64Parameter ¶
GetFloat64Parameter 取得由name指定的64位浮点数参数值
func GetInt32Parameter ¶
GetInt32Parameter 取得由name指定的32位整数参数值
func GetInt64Parameter ¶
GetInt64Parameter 取得由name指定的64位整数参数值
func GetURLRaw ¶
func GetURLRaw(client *http.Client, url string, params url.Values, reqHeader http.Header, cookies map[string]string) (header http.Header, body []byte, err error)
GetURLRaw 请求URL
func GetURLRawToWriter ¶
func GetURLRawToWriter(client *http.Client, url string, params url.Values, reqHeader http.Header, cookies map[string]string, writer io.Writer) (header http.Header, err error)
GetURLRawToWriter 请求URL
func GetURLRawToWriterWithContext ¶
func GetURLRawToWriterWithContext(ctx context.Context, client *http.Client, url string, params url.Values, reqHeader http.Header, cookies map[string]string, writer io.Writer) (header http.Header, err error)
GetURLRawToWriterWithContext 请求URL
func GetURLWithCookie ¶
func GetURLWithCookie(client *http.Client, url string, params url.Values, cookies map[string]string) (string, error)
GetURLWithCookie 请求URL
func NewRedirectError ¶
NewRedirectError redirect error
func ParseParams ¶
ParseParams 从r中解析参数,并填充到dest中,params应该是struct指针
func Post ¶ added in v1.1.35
func Post(client *http.Client, url string, params url.Values, header map[string]string, contentType string, requestBody io.Reader, cookies map[string]string, prepare PostPrepare) ([]byte, http.Header, error)
Post Http Post
func PostURL ¶
func PostURL(client *http.Client, url string, params url.Values, contentType string, requestBody io.Reader) ([]byte, http.Header, error)
PostURL 请求URL
func PostURLWithCookie ¶
func PostURLWithCookie(client *http.Client, url string, params url.Values, contentType string, requestBody io.Reader, cookies map[string]string) ([]byte, http.Header, error)
PostURLWithCookie 请求URL
func PostURLWithCookieAndHeader ¶
func PostURLWithCookieAndHeader(client *http.Client, url string, params url.Values, header map[string]string, contentType string, requestBody io.Reader, cookies map[string]string) ([]byte, http.Header, error)
PostURLWithCookieAndHeader 请求URL
func RegJSONMarshaler ¶
func RegJSONMarshaler(marshaler JSONMarshaler)
RegJSONMarshaler 注册JSON Marshaler
func RenderHTML ¶ added in v1.1.31
func RenderHTML(w http.ResponseWriter, data []byte)
RenderHTML 渲染HTML
func RenderTemplate ¶
func RenderTemplate(w http.ResponseWriter, templateDir, tmpl string, data interface{})
RenderTemplate 渲染模板
func RequestWithContext ¶
RequestWithContext 向req的context中设置key = val,返回新的request
func RequestWithError ¶
RequestWithError 向req中设置当前处理的错误,返回新的request
func ToUnderlineName ¶
ToUnderlineName 将驼峰命名改为小写的下划线命名
Types ¶
type BaseController ¶
type BaseController struct { Name string // Controller的名称 Path string // Controller的路径 HandlerMiddlewares map[string][]Middleware // Controller中需要使用middleware封装的方法 }
BaseController 表示一个控制器
func (*BaseController) GetHandlerMiddlewares ¶
func (p *BaseController) GetHandlerMiddlewares() map[string][]Middleware
GetHandlerMiddlewares handler的middleware
type Config ¶
type Config struct { Addr string //Http监听地址 ReadTimeout time.Duration //读超时,单位秒 WriteTimeout time.Duration //写超时,单位秒 MaxConns int //最大的并发连接数 // contains filtered or unexported fields }
Config Http配置
func (*Config) InitWithInjector ¶
InitWithInjector 初始化操作
func (*Config) RegController ¶
func (p *Config) RegController(controller Controller) error
RegController 注册controller中的所有处理函数
func (*Config) RegHandleFunc ¶
func (p *Config) RegHandleFunc(patternPath string, handlerFunc http.HandlerFunc) error
RegHandleFunc 注册patternPath的处理函数handlerFunc
func (*Config) RegMiddleware ¶
func (p *Config) RegMiddleware(middleware Middleware) error
RegMiddleware 注册middleware,middleware的注册需要在RegController和RegHandleFunc之前完成
type Controller ¶
type Controller interface { // GetName 控制器的名称 GetName() string // GetPath 路径前缀,以'/'结束,同一个控制下的http.Handler都 GetPath() string // GetHandlerMiddlewares 返回controller的处理方法中,需要增加middleware封装的方法,key是controller中的方法名 GetHandlerMiddlewares() map[string][]Middleware }
Controller 接口定义http处理器
type GraceableHandler ¶
type GraceableHandler struct {
// contains filtered or unexported fields
}
GraceableHandler 安全地关闭的处理器
func (*GraceableHandler) ServeHTTP ¶
func (p *GraceableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type JSONMarshaler ¶
JSONMarshaler json marshaler
type Middleware ¶
type Middleware interface { // Handle处理 Handle(next MiddlewareFunc) MiddlewareFunc }
Middleware 接口
type MiddlewareFunc ¶
type MiddlewareFunc func(http.ResponseWriter, *http.Request)
MiddlewareFunc middleware函数
type PostPrepare ¶ added in v1.1.35
type PostPrepare func(req *http.Request, body io.ReadSeeker) (err error)
PostPrepare prepare the post request before call client.Do(req)
type RedirectError ¶
type RedirectError struct {
RedirectURL string
}
RedirectError redirect error
func (*RedirectError) Error ¶
func (p *RedirectError) Error() string
type RequestError ¶
RequestError is the http request response error
func (*RequestError) Error ¶
func (p *RequestError) Error() string
type RequestMehotdMiddleware ¶
type RequestMehotdMiddleware struct { //允许的请求方法 AllowsMethods map[string]struct{} }
RequestMehotdMiddleware http请求方法
func NewRequestMetodMiddleware ¶
func NewRequestMetodMiddleware(methods ...string) *RequestMehotdMiddleware
NewRequestMetodMiddleware 用methods构建middleware
func (*RequestMehotdMiddleware) Handle ¶
func (p *RequestMehotdMiddleware) Handle(next MiddlewareFunc) MiddlewareFunc
Handle 校验Http请求的方法
type Resp ¶
type Resp struct { Success bool `json:"success"` Data interface{} `json:"data"` Msg string `json:"msg"` Code int32 `json:"code"` ExtData any `json:"ext_data,omitempty"` }
Resp JSON Http响应
type ResponseHandler ¶
type ResponseHandler struct { Success bool Data interface{} Msg string Cancel bool Code int32 ExtData any // contains filtered or unexported fields }
ResponseHandler 响应处理
func NewResponseHandler ¶
func NewResponseHandler(w http.ResponseWriter) *ResponseHandler
NewResponseHandler 构建响应处理
func (*ResponseHandler) FailWithMsg ¶
func (p *ResponseHandler) FailWithMsg(msg string)
FailWithMsg 设置失败及出错的消息
func (*ResponseHandler) SuccessWith ¶ added in v1.1.5
func (p *ResponseHandler) SuccessWith(data interface{}, msg string)
SuccessWith 设置成功,数据,消息
func (*ResponseHandler) SuccessWithData ¶
func (p *ResponseHandler) SuccessWithData(data interface{})
SuccessWithData 设置成功及数据
type RetrivedCookieJar ¶
type RetrivedCookieJar struct {
// contains filtered or unexported fields
}
RetrivedCookieJar 可持久化的Cookie
func NewRetrivedCookieJar ¶
func NewRetrivedCookieJar(o *cookiejar.Options) *RetrivedCookieJar
NewRetrivedCookieJar 构建PersistCookieJar
func (*RetrivedCookieJar) Cookies ¶
func (p *RetrivedCookieJar) Cookies(u *url.URL) []*http.Cookie
Cookies implements CookeJar.Cookies
func (*RetrivedCookieJar) SetCookies ¶
func (p *RetrivedCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)
SetCookies implements CookieJar.SetCookies
func (*RetrivedCookieJar) SetURLAndCookies ¶
func (p *RetrivedCookieJar) SetURLAndCookies(all map[string][]*http.Cookie) error
SetURLAndCookies 设置所有的URL和Cookie
func (*RetrivedCookieJar) URLAndCookies ¶
func (p *RetrivedCookieJar) URLAndCookies() map[string][]*http.Cookie
URLAndCookies 取得所有的URL和Cookie
type Service ¶
type Service struct { c.BaseService Conf *Config // contains filtered or unexported fields }
Service Http服务