Documentation
¶
Index ¶
- func Bind(fn interface{}, render Renderer) http.HandlerFunc
- func WithContext(parent context.Context, ctx *Context) context.Context
- type Context
- func (c *Context) Bind(r interface{}) error
- func (c *Context) ClientIP() string
- func (c *Context) ContentType() string
- func (c *Context) Context() context.Context
- func (c *Context) Cookie(name string) (string, bool)
- func (c *Context) Data(code int, contentType string, data []byte) error
- func (c *Context) File(filepath string)
- func (c *Context) FileAttachment(filepath, filename string)
- func (c *Context) FormParams() (url.Values, error)
- func (c *Context) Header(key string) (string, bool)
- func (c *Context) IndentedJSON(code int, obj interface{}) error
- func (c *Context) IndentedXML(code int, obj interface{}) error
- func (c *Context) IsWebsocket() bool
- func (c *Context) JSON(code int, obj interface{}) error
- func (c *Context) MultipartParams(maxMemory int64) (*multipart.Form, error)
- func (c *Context) PathParam(name string) (string, bool)
- func (c *Context) QueryParam(name string) (string, bool)
- func (c *Context) Redirect(code int, location string) error
- func (c *Context) RemoteIP() string
- func (c *Context) Render(code int, render render.Renderer) error
- func (c *Context) RequestBody() io.Reader
- func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) SetSameSite(samesite http.SameSite)
- func (c *Context) Status(code int)
- func (c *Context) String(code int, format string, args ...interface{}) error
- func (c *Context) XML(code int, obj interface{}) error
- type HttpError
- type MiddlewareFunc
- type Options
- type Renderer
- type RendererFunc
- type RouterGroup
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
func Bind(fn interface{}, render Renderer) http.HandlerFunc
Bind convert fn to HandlerFunc.
func(ctx context.Context)
func(ctx context.Context) R
func(ctx context.Context) error
func(ctx context.Context, req T) R
func(ctx context.Context, req T) error
func(ctx context.Context, req T) (R, error)
func(writer http.ResponseWriter, request *http.Request)
Types ¶
type Context ¶
type Context struct {
// A ResponseWriter interface is used by an HTTP handler to
// construct an HTTP response.
Writer http.ResponseWriter
// A Request represents an HTTP request received by a server
// or to be sent by a client.
Request *http.Request
// contains filtered or unexported fields
}
func FromContext ¶
func (*Context) Bind ¶
Bind checks the Method and Content-Type to select a binding engine automatically, Depending on the "Content-Type" header different bindings are used, for example:
"application/json" --> JSON binding "application/xml" --> XML binding
func (*Context) ClientIP ¶
ClientIP implements one best effort algorithm to return the real client IP. It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not. If it is it will then try to parse the headers defined in RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-Ip]). If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, the remote IP (coming from Request.RemoteAddr) is returned.
func (*Context) ContentType ¶
ContentType returns the request header `Content-Type`.
func (*Context) FileAttachment ¶
FileAttachment writes the specified file into the body stream in an efficient way On the client side, the file will typically be downloaded with the given filename
func (*Context) FormParams ¶
FormParams returns the form in the request.
func (*Context) IndentedJSON ¶
IndentedJSON serializes the given struct as pretty JSON (indented + endlines) into the response body. It also sets the Content-Type as "application/json".
func (*Context) IndentedXML ¶
IndentedXML serializes the given struct as pretty XML (indented + endlines) into the response body. It also sets the Content-Type as "application/xml".
func (*Context) IsWebsocket ¶
IsWebsocket returns true if the request headers indicate that a websocket handshake is being initiated by the client.
func (*Context) JSON ¶
JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json".
func (*Context) MultipartParams ¶
MultipartParams returns a request body as multipart/form-data. The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files.
func (*Context) QueryParam ¶
QueryParam returns the named query in the request.
func (*Context) RemoteIP ¶
RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).
func (*Context) RequestBody ¶
RequestBody returns the request body.
func (*Context) SetCookie ¶
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
SetCookie adds a Set-Cookie header to the ResponseWriter's headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
func (*Context) SetHeader ¶
SetHeader is an 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)`
func (*Context) SetSameSite ¶
SetSameSite with cookie
type MiddlewareFunc ¶
type MiddlewareFunc = mux.MiddlewareFunc
MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
type Options ¶
type Options struct {
// Addr optionally specifies the TCP address for the server to listen on,
// in the form "host:port". If empty, ":http" (port 8080) is used.
// The service names are defined in RFC 6335 and assigned by IANA.
// See net.Dial for details of the address format.
Addr string `value:"${addr:=}"`
// CertFile containing a certificate and matching private key for the
// server must be provided if neither the Server's
// TLSConfig.Certificates nor TLSConfig.GetCertificate are populated.
// If the certificate is signed by a certificate authority, the
// certFile should be the concatenation of the server's certificate,
// any intermediates, and the CA's certificate.
CertFile string `value:"${cert-file:=}"`
// KeyFile containing a private key file.
KeyFile string `value:"${key-file:=}"`
// ReadTimeout is the maximum duration for reading the entire
// request, including the body. A zero or negative value means
// there will be no timeout.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout time.Duration `value:"${read-timeout:=0s}"`
// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body. If ReadHeaderTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
ReadHeaderTimeout time.Duration `value:"${read-header-timeout:=0s}"`
// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout time.Duration `value:"${write-timeout:=0s}"`
// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled. If IdleTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
IdleTimeout time.Duration `value:"${idle-timeout:=0s}"`
// MaxHeaderBytes controls the maximum number of bytes the
// server will read parsing the request header's keys and
// values, including the request line. It does not limit the
// size of the request body.
// If zero, DefaultMaxHeaderBytes is used.
MaxHeaderBytes int `value:"${max-header-bytes:=0}"`
}
func (Options) GetCertificate ¶
func (options Options) GetCertificate(info *tls.ClientHelloInfo) (*tls.Certificate, error)
type RendererFunc ¶
func (RendererFunc) Render ¶
func (fn RendererFunc) Render(ctx *Context, err error, result interface{})
type RouterGroup ¶
type RouterGroup interface {
// Handler dispatches the handler registered in the matched route.
http.Handler
// BasePath returns the base path of router group.
BasePath() string
// Use appends a MiddlewareFunc to the chain.
Use(mwf ...MiddlewareFunc)
// Renderer to be used Response renderer in default.
Renderer(renderer Renderer)
// NotFound to be used when no route matches.
NotFound(handler http.Handler)
// MethodNotAllowed to be used when the request method does not match the route.
MethodNotAllowed(handler http.Handler)
// StrictSlash defines the trailing slash behavior for new routes. The initial
// value is false.
StrictSlash(value bool)
// SkipClean defines the path cleaning behaviour for new routes. The initial
// value is false. Users should be careful about which routes are not cleaned
//
// When true, if the route path is "/path//to", it will remain with the double
// slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/
//
// When false, the path will be cleaned, so /fetch/http://xkcd.com/534/ will
// become /fetch/http/xkcd.com/534
SkipClean(value bool)
// UseEncodedPath tells the router to match the encoded original path
// to the routes.
// For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".
//
// If not called, the router will match the unencoded path to the routes.
// For eg. "/path/foo%2Fbar/to" will match the path "/path/foo/bar/to"
UseEncodedPath()
// Group creates a new router group.
Group(path string) RouterGroup
// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
Any(path string, handler interface{}, r ...Renderer)
// Get registers a new GET route with a matcher for the URL path of the get method.
Get(path string, handler interface{}, r ...Renderer)
// Head registers a new HEAD route with a matcher for the URL path of the get method.
Head(path string, handler interface{}, r ...Renderer)
// Post registers a new POST route with a matcher for the URL path of the get method.
Post(path string, handler interface{}, r ...Renderer)
// Put registers a new PUT route with a matcher for the URL path of the get method.
Put(path string, handler interface{}, r ...Renderer)
// Patch registers a new PATCH route with a matcher for the URL path of the get method.
Patch(path string, handler interface{}, r ...Renderer)
// Delete registers a new DELETE route with a matcher for the URL path of the get method.
Delete(path string, handler interface{}, r ...Renderer)
// Connect registers a new CONNECT route with a matcher for the URL path of the get method.
Connect(path string, handler interface{}, r ...Renderer)
// Options registers a new OPTIONS route with a matcher for the URL path of the get method.
Options(path string, handler interface{}, r ...Renderer)
// Trace registers a new TRACE route with a matcher for the URL path of the get method.
Trace(path string, handler interface{}, r ...Renderer)
}
type Server ¶
type Server struct {
RouterGroup
// contains filtered or unexported fields
}
A Server defines parameters for running an HTTP server.
func (*Server) Run ¶
Run listens on the TCP network address Addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
func (*Server) Shutdown ¶
Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the Server's underlying Listener(s).