Context
type Context
type Context interface {
Context() stdctx.Context
// 返回当前请求的 `*http.Request` 结构体实例。
Request() *http.Request
// 设置 `*http.Request` 结构体实例。
SetRequest(r *http.Request)
// Response returns `slim.ResponseWriter`.
Response() ResponseWriter
// SetResponse sets `slim.ResponseWriter`.
SetResponse(r ResponseWriter)
// Logger returns the `Logger` instance.
Logger() Logger
// SetLogger Set the logger
SetLogger(logger Logger)
// Filesystem returns `fs.FS`.
Filesystem() fs.FS
// SetFilesystem sets `fs.FS`
SetFilesystem(fs.FS)
// IsTLS returns true if HTTP connection is TLS otherwise false.
IsTLS() bool
// IsWebSocket returns true if HTTP connection is WebSocket otherwise false.
IsWebSocket() bool
// Scheme returns the HTTP protocol scheme, `http` or `https`.
Scheme() string
// RealIP returns the client's network address based on `X-Forwarded-For`
// or `X-Real-IP` request header.
// The behavior can be configured using `Echo#IPExtractor`.
RealIP() string
RequestURI() string
// Accepts 返回支持的权重最高的媒体类型,若匹配失败则会返回空字符串。
// 给出的值可以是标准的媒体类型(如 application/json),也可以是扩展名(如 json、xml 等)。
Accepts(expect ...string) string
// AcceptsEncodings 返回支持的权重最高的编码方式,若匹配失败则会返回空字符串。
AcceptsEncodings(encodings ...string) string
// AcceptsCharsets 返回支持的权重最高的字符集,若匹配失败则会返回空字符串。
AcceptsCharsets(charsets ...string) string
// AcceptsLanguages 返回支持的权重最高的语言,若匹配失败则会返回空字符串。
AcceptsLanguages(languages ...string) string
// AllowsMethods 返回允许的请求方法
AllowsMethods() []string
// 返回路由器匹配的结果
RouteMatchType() RouteMatchType
// RouteInfo returns current request route information. Method, Path, Name and params if they exist for matched route.
// In the case of 404 (route not found) and 405 (method not allowed) RouteInfo returns generic struct for these cases.
RouteInfo() RouteInfo
// PathParam returns path parameter by name.
PathParam(name string) string
// PathParams returns path parameter values.
PathParams() PathParams
// SetPathParams set path parameter for during current request lifecycle.
SetPathParams(params PathParams)
// QueryParam returns the query param for the provided name.
QueryParam(name string) string
// QueryParams returns the query parameters as `url.Values`.
QueryParams() url.Values
// QueryString returns the URL query string.
QueryString() string
// FormValue returns the form field value for the provided name.
FormValue(name string) string
// FormParams returns the form parameters as `url.Values`.
FormParams() (url.Values, error)
// FormFile returns the multipart form file for the provided name.
FormFile(name string) (*multipart.FileHeader, error)
Header(key string) string
SetHeader(key string, values ...string)
// MultipartForm returns the multipart form.
MultipartForm() (*multipart.Form, error)
// Cookie returns the named cookie provided in the request.
Cookie(name string) (*http.Cookie, error)
// SetCookie adds a `Set-Cookie` header in HTTP response.
SetCookie(cookie *http.Cookie)
// Cookies return the HTTP cookies sent with the request.
Cookies() []*http.Cookie
// Get retrieves data from the context.
Get(key string) any
// Set saves data in the context.
Set(key string, val any)
// Bind binds the request body into a provided type `i`. The default binder
// does it based on Content-Type header.
Bind(i any) error
// Validate validates provided `i`. It is usually called after `Context#Bind()`.
// Validator must be registered using `Echo#Validator`.
Validate(i any) error
// Written returns whether the context response has been written to
Written() bool
// Render renders a template with data and sends a text/html response with status
// code. Renderer must be registered using `Echo.Renderer`.
Render(code int, name string, data any) error
// HTML sends an HTTP response with status code.
HTML(code int, html string) error
// HTMLBlob sends an HTTP blob response with status code.
HTMLBlob(code int, b []byte) error
// String sends a string response with status code.
String(code int, s string) error
// JSON sends a JSON response with status code.
JSON(code int, i any) error
// JSONPretty sends a pretty-print JSON with status code.
JSONPretty(code int, i any, indent string) error
// JSONBlob sends a JSON blob response with status code.
JSONBlob(code int, b []byte) error
// JSONP sends a JSONP response with status code. It uses `callback` to construct
// the JSONP payload.
JSONP(code int, callback string, i any) error
// JSONPBlob sends a JSONP blob response with status code. It uses `callback`
// to construct the JSONP payload.
JSONPBlob(code int, callback string, b []byte) error
// XML sends an XML response with status code.
XML(code int, i any) error
// XMLPretty sends a pretty-print XML with status code.
XMLPretty(code int, i any, indent string) error
// XMLBlob sends an XML blob response with status code.
XMLBlob(code int, b []byte) error
// Blob sends a blob response with a status code and content type.
Blob(code int, contentType string, b []byte) error
// Stream sends a streaming response with status code and content type.
Stream(code int, contentType string, r io.Reader) error
// File sends a response with the content of the file.
File(file string, filesystem ...fs.FS) error
// Attachment sends a response as attachment, prompting client to save the
// file.
Attachment(file string, name string) error
// Inline sends a response as inline, opening the file in the browser.
Inline(file string, name string) error
// NoContent sends a response with nobody and a status code.
NoContent(code ...int) error
// Redirect redirects the request to a provided URL with status code.
Redirect(code int, url string) error
// Error invokes the registered HTTP error handler.
// NB: Avoid using this method. It is better to return errors, so middlewares up in a chain could act on returned error.
Error(err error)
// Slim 返回 Slim 实例
Slim() *Slim
}
接口 Context
是表示当前 HTTP 请求的上下文,它包含对请求和响应对象的引用、路径、路径参数、数据和匹配的路由信息。
type EditableContext
type EditableContext interface {
Context
// RawPathParams returns raw path pathParams value.
RawPathParams() *PathParams
// SetRawPathParams replaces any existing param values with new values for this context lifetime (request).
SetRawPathParams(params *PathParams)
// SetRouteMatchType sets the RouteMatchType of router match for this request.
SetRouteMatchType(t RouteMatchType)
SetAllowsMethods(methods []string)
// SetRouteInfo sets the route info of this request to the context.
SetRouteInfo(ri RouteInfo)
// Reset resets the context after request completes. It must be called along
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
// See `Echo#ServeHTTP()`
Reset(w http.ResponseWriter, r *http.Request)
}
type PathParam
type PathParam struct {
Name string
Value string
}
路由参数单元结构体。
type PathParams
type PathParams []PathParam
func (PathParams) Get
func (p PathParams) Get(name string, defaultValue ...string) string
获取路参数,如果参数不存在,则使用给出的默认值,要是没有给出默认值,则返回空字符。
func (PathParams) Lookup
func (p PathParams) Lookup(name string) (string, bool)
获取路由参数,返回的第二个参数表示给出的名称是否存在。
type context
type context struct{}
默认上下文结构体,实现了 Context
和 EditableContext
这连个接口。
func (*context) Context()
func (c *context) Context()
返回 HTTP 请求上下文,也有是 *http.Request
的上下文,该方法实际上是:c.Request().Context()
。
func (*context) Request
func (c *context) Request(()
返回当前请求的 *http.Request
结构体实例。
func (*context) SetRequest
func (c *context) SetRequest()
设置 *http.Request
结构体实例。
func (*context) Response
func (c *context) Response() ResponseWriter
返回 slim.ResponseWriter
接口的实现,包装了 http.ResponseWriter
实例,同时实现了 http.Flusher
、http.Pusher
两个接口。
func (*context) SetResponse
func (c *context) SetResponse(r ResponseWriter)