为社区优秀的 Web 服务器提供一个抽象层,通过定义 Container
、Context
、Handler
、Filter
四大基本组件,使得底层实现可以灵活切换。
- Container
- Context
- NativeContext
- Get
- Set
- Request
- SetRequest
- Context
- IsTLS
- IsWebSocket
- Scheme
- ClientIP
- Path
- Handler
- ContentType
- GetHeader
- GetRawData
- PathParam
- PathParamNames
- PathParamValues
- QueryParam
- QueryParams
- QueryString
- FormValue
- FormParams
- FormFile
- SaveUploadedFile
- MultipartForm
- Cookie
- Cookies
- Bind
- ResponseWriter
- Status
- Header
- SetCookie
- NoContent
- String
- HTML
- HTMLBlob
- JSON
- JSONPretty
- JSONBlob
- JSONP
- JSONPBlob
- XML
- XMLPretty
- XMLBlob
- Blob
- File
- Attachment
- Inline
- Redirect
- SSEvent
- Handler
- 路由风格
- 全局变量
定义一个 Web 服务器,具有注册路由、设置中间件、注册 Swagger 响应器等功能。
返回和 Mapping 绑定的路由分组。
Route(basePath string, filters ...Filter) *Router
注册任意 HTTP 方法处理函数。
HandleRequest(method uint32, path string, fn Handler, filters ...Filter) *Mapper
注册任意 HTTP 方法处理函数。
RequestMapping(method uint32, path string, fn HandlerFunc, filters ...Filter) *Mapper
注册任意 HTTP 方法处理函数。
RequestBinding(method uint32, path string, fn interface{}, filters ...Filter) *Mapper
注册 GET 方法处理函数。
HandleGet(path string, fn Handler, filters ...Filter) *Mapper
注册 GET 方法处理函数。
GetMapping(path string, fn HandlerFunc, filters ...Filter) *Mapper
注册 GET 方法处理函数。
GetBinding(path string, fn interface{}, filters ...Filter) *Mapper
注册 POST 方法处理函数。
HandlePost(path string, fn Handler, filters ...Filter) *Mapper
注册 POST 方法处理函数。
PostMapping(path string, fn HandlerFunc, filters ...Filter) *Mapper
注册 POST 方法处理函数。
PostBinding(path string, fn interface{}, filters ...Filter) *Mapper
注册 PUT 方法处理函数。
HandlePut(path string, fn Handler, filters ...Filter) *Mapper
注册 PUT 方法处理函数。
PutMapping(path string, fn HandlerFunc, filters ...Filter) *Mapper
注册 PUT 方法处理函数。
PutBinding(path string, fn interface{}, filters ...Filter) *Mapper
注册 DELETE 方法处理函数。
HandleDelete(path string, fn Handler, filters ...Filter) *Mapper
注册 DELETE 方法处理函数。
DeleteMapping(path string, fn HandlerFunc, filters ...Filter) *Mapper
注册 DELETE 方法处理函数。
DeleteBinding(path string, fn interface{}, filters ...Filter) *Mapper
添加过滤器。
AddFilter(filter ...Filter)
设置 Logger Filter。
SetLoggerFilter(filter Filter)
添加新的路由信息。
AddRouter(router RootRouter)
返回和容器绑定的 Swagger 对象。
Swagger() *Swagger
启动 Web 容器。
Start() error
停止 Web 容器。
Stop(ctx context.Context) error
封装 *http.Request 和 http.ResponseWriter 对象,简化操作接口。
返回封装的底层上下文对象。
NativeContext() interface{}
retrieves data from the context.
Get(key string) interface{}
saves data in the context.
Set(key string, val interface{})
returns *http.Request
.
Request() *http.Request
sets *http.Request
.
SetRequest(r *http.Request)
返回 Request 绑定的 context.Context 对象。
Context() context.Context
returns true if HTTP connection is TLS otherwise false.
IsTLS() bool
returns true if HTTP connection is WebSocket otherwise false.
IsWebSocket() bool
returns the HTTP protocol scheme, http
or https
.
Scheme() string
implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
ClientIP() string
returns the registered path for the handler.
Path() string
returns the matched handler by router.
Handler() Handler
returns the Content-Type header of the request.
ContentType() string
returns value from request headers.
GetHeader(key string) string
return stream data.
GetRawData() ([]byte, error)
returns path parameter by name.
PathParam(name string) string
returns path parameter names.
PathParamNames() []string
returns path parameter values.
PathParamValues() []string
returns the query param for the provided name.
QueryParam(name string) string
returns the query parameters as url.Values
.
QueryParams() url.Values
returns the URL query string.
QueryString() string
returns the form field value for the provided name.
FormValue(name string) string
returns the form parameters as url.Values
.
FormParams() (url.Values, error)
returns the multipart form file for the provided name.
FormFile(name string) (*multipart.FileHeader, error)
uploads the form file to specific dst.
SaveUploadedFile(file *multipart.FileHeader, dst string) error
returns the multipart form.
MultipartForm() (*multipart.Form, error)
returns the named cookie provided in the request.
Cookie(name string) (*http.Cookie, error)
returns the HTTP cookies sent with the request.
Cookies() []*http.Cookie
binds the request body into provided type i
. The default binder does it based on Content-Type header.
Bind(i interface{}) error
returns http.ResponseWriter
.
ResponseWriter() ResponseWriter
sets the HTTP response code.
Status(code int)
is a intelligent shortcut for c.Writer.Header().Set(key, value). It writes a header in the response. If value == "",
this method removes the header c.Writer.Header().Del(key)
Header(key, value string)
adds a Set-Cookie
header in HTTP response.
SetCookie(cookie *http.Cookie)
sends a response with no body and a status code. Maybe panic.
NoContent()
writes the given string into the response body. Maybe panic.
String(format string, values ...interface{})
sends an HTTP response with status code. Maybe panic.
HTML(html string)
sends an HTTP blob response with status code. Maybe panic.
HTMLBlob(b []byte)
sends a JSON response with status code. Maybe panic.
JSON(i interface{})
sends a pretty-print JSON with status code. Maybe panic.
JSONPretty(i interface{}, indent string)
sends a JSON blob response with status code. Maybe panic.
JSONBlob(b []byte)
sends a JSONP response with status code. It uses callback
to construct the JSONP payload. Maybe panic.
JSONP(callback string, i interface{})
sends a JSONP blob response with status code. It uses
callback
to construct the JSONP payload. Maybe panic.
JSONPBlob(callback string, b []byte)
sends an XML response with status code. Maybe panic.
XML(i interface{})
sends a pretty-print XML with status code. Maybe panic.
XMLPretty(i interface{}, indent string)
sends an XML blob response with status code. Maybe panic.
XMLBlob(b []byte)
sends a blob response with status code and content type. Maybe panic.
Blob(contentType string, b []byte)
sends a response with the content of the file. Maybe panic.
File(file string)
sends a response as attachment, prompting client to save the file. Maybe panic.
Attachment(file string, name string)
sends a response as inline, opening the file in the browser. Maybe panic.
Inline(file string, name string)
redirects the request to a provided URL with status code. Maybe panic.
Redirect(url string)
writes a Server-Sent Event into the body stream. Maybe panic.
SSEvent(name string, message interface{})
以函数的方式实现 Handler。
标准 Web 处理函数的辅助函数。
func FUNC(fn HandlerFunc) Handler
标准 Http 处理函数的辅助函数。
func HTTP(fn http.HandlerFunc) Handler
标准 Http 处理函数的辅助函数。
func WrapF(fn http.HandlerFunc) Handler
标准 Http 处理函数的辅助函数。
func WrapH(h http.Handler) Handler
转换成 BIND 形式的 Web 处理接口。
func BIND(fn interface{}) Handler
提供 echo、gin 和 {} 三种可无缝切换的路由风格:/a/:b/c/:d/*
是 echo 风格;/a/:b/c/:d/*e
是 gin 风格;/a/{b}/c/{e:*}
、/a/{b}/c/{*:e}
、/a/{b}/c/{*}
是 {} 风格。
全局参数校验器。
自定义错误处理函数。
全局的日志过滤器,Container 如果没有设置日志过滤器则会使用全局的日志过滤器。